Guest User

Untitled

a guest
Jan 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Oz 0.90 KB | None | 0 0
  1. %List2
  2. %2
  3. declare fun {Fib1 N}
  4.        if N==0 then 0 elseif N==1 then 1 else {Fib1 N-1}+{Fib1 N-2} end end
  5.  
  6. {Show {Fib1 37}}
  7. %approx 10 secs
  8.  
  9. declare fun {Fib2 N}
  10.        local fun {TailFib N Acc Next}
  11.             if N == 0 then Acc else {TailFib N-1 Next Acc+Next}
  12.             end
  13.          end
  14.        in {TailFib N 0 1}
  15.        end
  16.     end
  17.        
  18. {Show {Fib2 37}}
  19. %Less than 1 sec
  20.  
  21. %4.
  22. declare X = 0
  23. %a)
  24. declare _|_|X|_|_|nil = ~2|~1|0|1|2|nil
  25. %b)
  26. declare _#_|X#_|nil = 1#2|0#1|nil
  27.  
  28. %5.
  29. declare fun {Initsegment L1#L2}
  30.        case L1 of
  31.           nil then true
  32.        [] H|T then if L2 == nil then false
  33.                elseif H == L2.1 then {Initsegment T#L2.2} else false
  34.                end
  35.        end
  36.     end
  37.  
  38. %6.
  39. declare fun {Replace_nth L#N#V}
  40.        if L == nil orelse {Length L}< N then raise 'Incorrect Arguments' end
  41.        elseif N == 0 then V|L.2 else L.1|{Replace_nth L.2#N-1#V}
  42.        end
  43.     end
  44. {Show {Replace_nth (nil)#0#0}}
Add Comment
Please, Sign In to add comment