Advertisement
niktkolwiek

L PP Lista 9 OCaml

Dec 5th, 2011
2,995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*Zad1*)
  2. type 'a llist = LNil | LCons of 'a * (unit -> 'a llist);;
  3.  
  4. let lengthll ll =
  5.   let rec licz (l,acc) =
  6. match l with
  7.   | LCons(x,xf) -> licz (xf(),acc+1)
  8.   | LNil -> acc
  9.   in licz(ll,0);;
  10.  
  11. let cut (ll,p,k)=
  12.   let rec licz (l,n,acc) =
  13. match l with
  14.   | LCons(x,xf) ->
  15. if n>0 then licz(xf(),n-1,acc)
  16. else licz(xf(),n-1,LCons(x,function() -> acc))
  17.   | LNil -> acc
  18.   in licz(licz(ll,p,LNil),k,LNil);;
  19.  
  20. let rec ltake = function
  21. (0,xq) -> []
  22.   | (n, LNil) -> []
  23.   | (n, LCons(x,xf)) -> x::ltake(n-1,xf());;
  24.  
  25. let leniwa = LCons(1,
  26. function()-> LCons(2,
  27. function()-> LCons(3,
  28. function()-> LCons(4,
  29. function()-> LCons(5,
  30. function()-> LNil)
  31. ))));;
  32.  
  33. ltake(10, cut(leniwa,0,0));;
  34.  
  35. (*Zad2*)
  36. type ('a, 'b) slowa = Wyraz of 'a * 'b;;
  37.  
  38. let insert (slownik,slowo) =
  39.   let rec ins snk =
  40.     match snk with
  41.       | h::t ->
  42.       let Wyraz(swo,wyst) = h in
  43.       if swo=slowo then Wyraz(swo,wyst+1)::t
  44.       else if slowo<swo then Wyraz(slowo,1)::h::t
  45.       else h::ins t
  46.       | [] -> [Wyraz(slowo,1)]
  47.   in ins slownik;;
  48.  
  49. insert(insert(insert(insert(insert([],1),2),2),3),1);;
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement