Guest User

Untitled

a guest
Apr 15th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Oz 1.62 KB | None | 0 0
  1. % <LList T> ::= lNil | lCons(T ({} -> <LList T>))
  2.  
  3. declare fun {LIter F X} lCons(X fun {$}{LIter F {F X}} end) end
  4.  
  5. declare fun {LFrom K}{LIter fun {$ N} N+1 end K} end
  6.    
  7. declare
  8. fun {LTake N LL}
  9.    case N#LL
  10.    of 0#_ then nil
  11.    [] _#lNil then nil
  12.    [] M#lCons(X LT) then X|{LTake M-1 {LT}}
  13.    end
  14. end
  15.  
  16.  
  17. declare BST=node("ekran"#"screen"
  18.          node("adapter"#"adapter"
  19.               empty
  20.               node("klawiatura"#"keyboard"
  21.                empty
  22.                empty
  23.               )
  24.              )
  25.          node("komputer"#"computer"
  26.               empty
  27.               node("mysz"#"mouse"
  28.                empty
  29.                empty
  30.               )
  31.              )
  32.         )
  33.  
  34. fun {Switch BT}
  35.    case BT of
  36.       empty then empty
  37.    [] node(V1#V2 L R) then node(V2#V1 {Switch L} {Switch R})
  38.    end
  39. end
  40.  
  41. {Inspect {Switch BST}}
  42.  
  43. fun {Fibonacci}
  44.    local
  45.       fun {Fib A#B}
  46.          lCons(A fun {$}{Fib A+B#A} end)
  47.       end
  48.    in {Fib 0#1}
  49.    end
  50. end
  51.  
  52. {Browse {LTake 6 {Fibonacci}}}
  53.  
  54.  
  55. declare
  56. fun {LDivide LL}
  57.    local
  58.       fun {Divide LL Mod W}
  59.      case LL
  60.      of lNil then lNil
  61.      [] lCons(X LT) then
  62.         case W
  63.         of true then if X mod Mod == 0 then {Divide {LT} Mod W} else
  64.                 lCons(X fun{$}{Divide {LT} Mod W}end)end
  65.         [] false then if X mod Mod == 0 then lCons(X fun{$}{Divide {LT} Mod W}end) else
  66.                  {Divide {LT} Mod W} end
  67.         end
  68.      end
  69.       end
  70.    in{Divide LL 3 false}#{Divide LL 3 true}
  71.    end
  72. end
  73.  
  74.  
  75.  
  76. declare
  77. fun {LTakePair LL1#LL2 N1 N2}
  78.    {LTake N1 LL1}#{LTake N2 LL2}
  79. end
  80.  
  81. {Browse {LTakePair {LDivide {LFrom 1}} 7 7}}
  82.  
  83. declare
  84. fun {LFoldL LL F Acc N}
  85.    case LL#N
  86.    of _#0  then Acc
  87.    [] lNil#_ then Acc
  88.    [] lCons(X LT)#_ then {LFoldL {LT} F {F Acc X} (N-1)}
  89.    end
  90. end
  91.  
  92. {Browse {LFoldL {Fibonacci} fun{$Acc X} Acc+X end 0 5}}
Add Comment
Please, Sign In to add comment