%%%%%%%%%%%%%%%%%%%%%%%% %Avoiding Memory Leaks % %%%%%%%%%%%%%%%%%%%%%%%% local fun {Sum X L1 L} case L1 of Y|L2 then {Sum X+Y L2 L} else X end end in local L = [1 2 3] in {Browse {Sum 0 L L}} end end local fun {Sum X L1} case L1 of Y|L2 then {Sum X+Y L2} else X end end in local L = [1 2 3] in {Browse {Sum 0 L}} end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% declare Fibonacci = fun {$ N} if N == 0 then 0 elseif N == 1 then 1 else {Fibonacci N-1}+{Fibonacci N-2} end end {Browse {Fibonacci 15}} {Browse {Fibonacci 22}} {Browse {Fibonacci 30}} {Browse {Fibonacci 40}} declare TFibonacci = fun {$ N} if N == 0 then 0 elseif N == 1 then 1 else thread {TFibonacci N-1} end + thread {TFibonacci N-2} end end end {Browse {TFibonacci 15}} {Browse {TFibonacci 22}} {Browse {TFibonacci 25}} {Browse {TFibonacci 27}} {Browse {TFibonacci 28}} declare FibAcc = fun {$ N M A1 A2} if N==M then A1+A2 else {FibAcc N M+1 A2 A1+A2} end end declare Fib = fun {$ N} if N =< 1 then N else {FibAcc N 2 0 1} end end {Browse {Fib 6}} {Browse {Fib 30}} {Browse {Fib 100}} declare FibAcc = fun {$ N A1 A2} if N==2 then A1+A2 else {FibAcc N-1 A2 A1+A2} end end declare Fib = fun {$ N} if N =< 1 then N else {FibAcc N 0 1} end end {Browse {Fib 6}} {Browse {Fib 30}} {Browse {Fib 100}}