% Square root v1 declare fun {Abs X} if X < 0.0 then ~X else X end end fun {Improve Guess X} (Guess + X/Guess)/2.0 end fun {GoodEnough Guess X} {Abs X-Guess*Guess}/X < 0.00001 end fun {SqrtIter Guess X} if {GoodEnough Guess X} then Guess else {SqrtIter {Improve Guess X} X} end end fun {Sqrt X} Guess = 1.0 in {SqrtIter Guess X} end {Browse {Sqrt 10.0}} % Square root v2 local fun {SqrtIter Guess X} if {GoodEnough Guess X} then Guess else {SqrtIter {Improve Guess X} X} end end fun {Improve Guess X} (Guess + X/Guess)/2.0 end fun {GoodEnough Guess X} {Abs X - Guess*Guess}/X < 0.000001 end in fun {Sqrt X} Guess = 1.0 in {SqrtIter Guess X} end end {Browse {Sqrt 10.0}} % Square root v3 local fun {SqrtIter Guess X} fun {Improve} (Guess + X/Guess)/2.0 end fun {GoodEnough} {Abs X - Guess*Guess}/X < 0.000001 end in if {GoodEnough} then Guess else {SqrtIter {Improve} X} end end in fun {Sqrt X} Guess = 1.0 in {SqrtIter Guess X} end end {Browse {Sqrt 10.0}} % Square root final version fun {Sqrt X} fun {Improve Guess} (Guess + X/Guess)/2.0 end fun {GoodEnough Guess} {Abs X - Guess*Guess}/X < 0.000001 end fun {SqrtIter Guess} if {GoodEnough Guess} then Guess else {SqrtIter {Improve Guess} } end end Guess = 1.0 in {SqrtIter Guess} end {Browse {Sqrt 10.0}} %SumList declare fun {FoldFactory F U} fun {FoldR L F U} case L of nil then U [] X|L2 then {F X {FoldR L2 F U}} end end in fun {$ L} {FoldR L F U} end end declare SumList={FoldFactory fun {$ A B} A+B end 0} {Browse {SumList [1 2 3]}} %for I in 1..10 do {Browse I} declare proc {For I J P} if I >= J then skip else {P I} {For I+1 J P} end end {For 1 10 Browse} % declare proc {ForAll Xs P} case Xs of nil then skip [] X|Xr then {P X} {ForAll Xr P} end end {ForAll [m n s t] proc{$ I} {System.showInfo "the item is: " # I} end} declare for I in [a b c d] do {System.showInfo "the item is: " # I} end %map declare fun {Map Xs F} case Xs of nil then nil [] X|Xr then {F X}|{Map Xr F} end end {Browse {Map [ 1 2 3 4] fun {$ I} I*I end}} %filter declare fun {Filter Xs P} case Xs of nil then nil [] X|Xr andthen {P X} then X|{Filter Xr P} [] X|Xr then {Filter Xr P} end end {Browse {Filter [1 2 3 4] fun {$ A} A<3 end}} %DFS tree declare proc {DFS Tree} case Tree of tree(node:N sons:Sons ...) then {Browse N} for T in Sons do {DFS T} end end end T=tree(node:1 sons:[tree(node:2 sons:nil) tree(node:3 sons:[tree(node:4 sons:nil)])]) {DFS T} % declare fun {FoldL Xs F U} case Xs of nil then U [] X|Xr then {FoldL Xr F {F X U}} end end {Browse {FoldL [1 2 3] fun {$ X Y} X|Y end nil}}