In Scheme, how do you use lambda to create a recursive function?

后端 未结 8 1013
走了就别回头了
走了就别回头了 2020-12-03 01:17

I\'m in a Scheme class and I was curious about writing a recursive function without using define. The main problem, of course, is that you cannot call a function within itse

8条回答
  •  失恋的感觉
    2020-12-03 01:34

    I was curious about writing a recursive function without using define. The main problem, of course, is that you cannot call a function within itself if it doesn't have a name.

    A little off-topic here, but seeing the above statements I just wanted to let you know that "without using define" does not mean "doesn't have a name". It is possible to give something a name and use it recursively in Scheme without define.

    (letrec
      ((fact
         (lambda (n)
           (if (zero? n)
             1
             (* n (fact (sub1 n)))))))
      (fact 5))
    

    It would be more clear if your question specifically says "anonymous recursion".

提交回复
热议问题