Skip to content

Instantly share code, notes, and snippets.

@mnicky
Created August 23, 2011 12:10
Show Gist options
  • Save mnicky/1164956 to your computer and use it in GitHub Desktop.
Save mnicky/1164956 to your computer and use it in GitHub Desktop.
How to make an anonymous function call itself
;we define a function 'starter' which calls its first parameter on itself and the second parameter
(define starter
(lambda (f arg)
((f f arg))))
;we call the 'starter' on anonymous (lambda) function (calling its first parameter on itself and the second parameter) and a number
(starter (lambda (g num)
(printf "~a\n" num)
(g g (+ num 1)))
0)
;the lambda will call itself and will run forewer (with tail call optimalization), printing ascending numbers
;...and calling it all together at the point of definition
((lambda (f arg)
((f f arg)))
(lambda (g num)
(printf "~a\n" num)
(g g (+ num 1)))
0)
@HARISHWA2003
Copy link

Can u explain it plz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment