42 lines
710 B
Text
42 lines
710 B
Text
1) defining the Ycombinator
|
|
{def Y
|
|
{lambda {:f :n}
|
|
{:f :f :n}}}
|
|
|
|
2) defining non recursive functions
|
|
2.1) factorial
|
|
{def almost-fac
|
|
{lambda {:f :n}
|
|
{if {= :n 1}
|
|
then 1
|
|
else {* :n {:f :f {- :n 1}}}}}}
|
|
|
|
2.2) fibonacci
|
|
{def almost-fibo
|
|
{lambda {:f :n}
|
|
{if {< :n 2}
|
|
then 1
|
|
else {+ {:f :f {- :n 1}} {:f :f {- :n 2}}}}}}
|
|
|
|
3) testing
|
|
{Y almost-fac 6}
|
|
-> 720
|
|
{Y almost-fibo 8}
|
|
-> 34
|
|
|
|
We could also forget the Ycombinator and names:
|
|
|
|
1) fac:
|
|
{{lambda {:f :n} {:f :f :n}}
|
|
{lambda {:f :n}
|
|
{if {= :n 1}
|
|
then 1
|
|
else {* :n {:f :f {- :n 1}}}}} 6}
|
|
-> 720
|
|
|
|
2) fibo:
|
|
{{lambda {:f :n} {:f :f :n}}
|
|
{{lambda {:f :n}
|
|
{if {< :n 2} then 1
|
|
else {+ {:f :f {- :n 1}} {:f :f {- :n 2}}}}}} 8}
|
|
-> 34
|