Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'let': no recursive functions #14

Open
wboeke opened this issue Oct 19, 2021 · 2 comments
Open

'let': no recursive functions #14

wboeke opened this issue Oct 19, 2021 · 2 comments

Comments

@wboeke
Copy link

wboeke commented Oct 19, 2021

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?

@tau-dev
Copy link

tau-dev commented Oct 27, 2021

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)
))

@ooichu
Copy link

ooichu commented Feb 21, 2023

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)
))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants