2 2

Named lets in Fennel

Details
Message ID
<20190118213313.GB25766@airmail.cc>
DKIM signature
pass
Download raw message
Scheme has a concept of "named lets", essentially a let you can call
from within itself, which is useful for recursion.

R5RS [1]:
> (let <variable> <bindings> <body>)
>
> "Named let" is a variant on the syntax of let which provides a more
> general looping construct than do and may also be used to express
> recursions. It has the same syntax and semantics as ordinary let
> except that <variable> is bound within <body> to a procedure whose
> formal arguments are the bound variables and whose body is <body>.
> Thus the execution of <body> may be repeated by invoking the procedure
> named by <variable>.

Example Fennel pseudocode that prints numbers 0-9:

  (let loop [i 0]
    (if (< i 10)
      (print i)
      (loop (+ i 1))))

Are there plans to implement a similar construct in Fennel?

[1] https://schemers.org/Documents/Standards/R5RS/
Justin Smith
Details
Message ID
<CAGokn9Lc5ZAfn-mLJLGuKSPbR_sMcMqoLxwDxZB5pr0cSF4esA@mail.gmail.com>
In-Reply-To
<20190118213313.GB25766@airmail.cc> (view parent)
DKIM signature
pass
Download raw message
On Fri, Jan 18, 2019 at 1:32 PM Jason Valencia <jasval@airmail.cc> wrote:
>
> Scheme has a concept of "named lets", essentially a let you can call
> from within itself, which is useful for recursion.
>
> R5RS [1]:
> > (let <variable> <bindings> <body>)

so is this effectively (fn x [] ...) except x is only visible inside
the fn, and not to the surrounding environment?

if so, one approach would be a macro that generates ((fn [] (fn x
[arg] ...) nil))

something like (defmacro named-fn [name bindings ...] `((fn [] (fn
@name @bindings @...)))

(of course name and bindings could be collapsed into ... but this
implementation makes it more clear what's going on)
Details
Message ID
<87won1wj4s.fsf@hagelb.org>
In-Reply-To
<CAGokn9Lc5ZAfn-mLJLGuKSPbR_sMcMqoLxwDxZB5pr0cSF4esA@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message
Justin Smith <noisesmith@gmail.com> writes:

> On Fri, Jan 18, 2019 at 1:32 PM Jason Valencia <jasval@airmail.cc> wrote:
>>
>> Scheme has a concept of "named lets", essentially a let you can call
>> from within itself, which is useful for recursion.
>>
>> R5RS [1]:
>> > (let <variable> <bindings> <body>)
>
> so is this effectively (fn x [] ...) except x is only visible inside
> the fn, and not to the surrounding environment?

Yeah, I would say we already have this construct more or less. Is there
something else to it I'm missing? It doesn't seem like a good idea to
add unrelated functionality to `let' just to avoid using `fn'.

-Phil