fe icon indicating copy to clipboard operation
fe copied to clipboard

'let': no recursive functions

Open wboeke opened this issue 3 years ago • 2 comments

What is a lisp without recursion? Not very useful. Therefore it is a pity that with fe you cannot specify a recursive function, using 'let'. You are forced to use '=', but then the function gets global scope which is not always wanted. Maybe there is a small modification possible to solve this problem?

wboeke avatar Oct 19 '21 16:10 wboeke

You can have a recursive local function by first declaring the variable with let and then setting it, e.g. with such a macro:

(= reclet (mac (name def)
  (list 'let name)
  (list '= name def)
))

tau-dev avatar Oct 27 '21 21:10 tau-dev

You can have a recursive local function by first declaring the variable with let and then setting it, e.g. with such a macro:

(= reclet (mac (name def)
  (list 'let name)
  (list '= name def)
))

This is wrong! This macro does not actually create a local variable, it works literally as (= name def) because (list 'let name) (by the way, (let name) raises 'too few arguments' error) form is omitted. If you want to make a macro that evaluates multiple forms, you have to use do. Then, we get the following:

(= reclet (mac (name def)
  (list 'do
    (list 'let name)
    (list '= name def)
  )
))

But that macro is useless. So, it looks like in fe you can't create a macro that creates a local variable and does something else in the calling environment.

What is a lisp without recursion? Not very useful. Therefore it is a pity that with fe you cannot specify a recursive function, using 'let'. You are forced to use '=', but then the function gets global scope which is not always wanted. Maybe there is a small modification possible to solve this problem?

You can do this following way (just a spontaneous example):

(= fib-sqr (fn (x)
  ; create local recursive function
  (let fib nil)
  (= fib (fn (x)
    (if (<= 2 x) (+ (fib (- x 2)) (fib (- x 1))) x)
  ))
  ; call it
  (let n (fib x))
  (* n n)
))

ooichu avatar Feb 21 '23 19:02 ooichu