Author Topic: Closed: Test yourself (for fun). Round 4. The winner is EUGENY L. YAKIMOVITCH :)  (Read 1584 times)

0 Members and 1 Guest are viewing this topic.

divtiply

  • Guest
Write a _recursive lambda_ function in AutoLISP without using assignments such as setq/defun
« Last Edit: December 24, 2014, 12:07:52 PM by divtiply »

ymg

  • Guest
Re: Test yourself (for fun). Round 4.
« Reply #1 on: December 23, 2014, 09:03:58 PM »
The following will return the factorial of 4:

Code - Auto/Visual Lisp: [Select]
  1. ((lambda (lfact n) (lfact n lfact)) (lambda (n lfact) (cond ((> n 0) (* n (lfact (- n 1) lfact))) (t 1) ) ) 4)
  2.  

I cheated, it's from a guy named  EUGENY L. YAKIMOVITCH

Here: http://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/27%20recursive-lambda-factorial-in-lisp.html

And a slightly better explanation for why it works on page 5 of the following:

http://cs.brown.edu/courses/cs173/2001/Lectures/2001-10-25.pdf


ymg
« Last Edit: December 23, 2014, 11:14:09 PM by ymg »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Test yourself (for fun). Round 4.
« Reply #2 on: December 24, 2014, 05:55:42 AM »
I would simplify ymg's suggestion to:
Code: [Select]
(
  (lambda (lfact n) (lfact n))
  (lambda (n)
    (cond
      ((> n 1)
        (* n (lfact (- n 1)))
      )
      (t
        1
      )
    )
  )
  4
)

But IMO assignment still takes place in the outside lambda: the argument lfact is assigned a function definition.
« Last Edit: December 24, 2014, 09:53:37 AM by roy_043 »

ymg

  • Guest
Re: Test yourself (for fun). Round 4.
« Reply #3 on: December 24, 2014, 10:37:33 AM »
roy043,

The simplification you propose certainly works, but means as you said
that it takes place outside the lambda.

The generic form of what they call the Y function or Y-combinator
from the paper by  Don Blaheta is:

Quote
(lambda (f) ((lambda (x) (x x)) (lambda (x) (f (x x)))))

Works by recreating a lambda at every step of the recursion.

I am not too sure it is very useful though, but mind boggling nonetheless.

ymg

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
@ ymg:
By 'in the outside lambda' I mean: 'inside the outermost lambda'.