Left factorials: Difference between revisions

Content added Content deleted
(→‎{{header|Lambdatalk}}: flagged Lamdatalk as incorrect (wrong start of the 1st range).)
No edit summary
Line 1,868: Line 1,868:
=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==


{{incorrect|Lambdatalk| <br><br> The first range of left factorials should start with 0 (zero), not 2. <br><br> }}
{{incorrect|Lambdatalk| <br><br> The first range of left factorials should start with 0 (zero), not 2. <br><br>
<center>Hi Rosetta, I fixed the code. Thanks for the warning, it gave me a chance to improve it. At least that's what I think.</center>
}}


The code can be tested in this wiki page: http://lambdaway.free.fr/lambdawalks/?view=left_factorial
<lang scheme>
<lang scheme>
2.1) defining !n
2.1) defining !n


{def !n // the main function's name
{def MEM {A.new 1}}
{def MEM.get {lambda {:n} {A.get :n {MEM}}}}
{def MEM.set! {lambda {:n :val} {A.get :n {A.set! :n :val {MEM}}}}}
{def MEM.undef? {lambda {:n} {W.equal? {A.get :n {MEM}} undefined} }}


{def !n.mem {A.new 0 1 2}} // initializing a global array
{def !n
// memorizing the computed values of !n
{def n!
{def !n.set // computing and storing the values
{lambda {:n}
{lambda {:n}
{A.set! :n // assign at n
{if {MEM.undef? :n}
then {MEM.set! :n {long_mult :n {MEM.get {- :n 1}}}}
{long_mult :n {A.get {- :n 1} {!n.mem}} } // product of n and computed value at n-1
{!n.mem}}}} // in the global array
else {MEM.get :n} }} }

{lambda {:n}
{def !n.get // getting a value
{S.reduce long_add 1 {S.map n! {S.serie 1 {- :n 1}}}}}}
{lambda {:n}
{A.get :n {if {W.equal? {A.get :n {!n.mem}} undefined} // if it doesn't exist
then {!n.set :n} // then compute it
else {!n.mem}}}}} // else get it from the global array

{lambda {:n} // the main function's body
{if {< :n 2} // if n=0 and n=1
then :n // then return 0 or 1
else {S.reduce long_add // apply add_long to
1 {S.map !n.get {S.serie 1 {- :n 1}}}}}}} // the sequence of computed values


2.2) the task
2.2) the task
Line 1,892: Line 1,903:


{S.map {lambda {:n} {br}!n(:n) = {!n :n}}
{S.map {lambda {:n} {br}!n(:n) = {!n :n}}
{S.serie 2 10}}
{S.serie 0 10}}
->
->
!n(0) = 0
!n(1) = 1
!n(2) = 2
!n(2) = 2
!n(3) = 4
!n(3) = 4