Facebook
From Denim Porcupine, 6 Years ago, written in OCaml (Objective Caml).
This paste is a reply to Re: Untitled from Wet Butterfly - view diff
Embed
Download Paste or View Raw
Hits: 366
  1. let xs = 1 :: 2 :: 3 :: 4 :: [];;
  2.  
  3. let rec nTy = function (xs, n) ->
  4.         if (xs = []) then raise (Failure "Error")
  5.         else if (n = 1) then List.hd xs
  6.         else nTy(List.tl xs, n - 1);;
  7. nTy(xs, 2);;
  8.  
  9.  
  10. let rec podzielPoN = function (xs, n) ->
  11.        
  12.         let rec podzielPoN_it = function (a, b, n) ->
  13.                 if (n = 0) then (b, a)
  14.                 else podzielPoN_it(List.tl a, b @ (List.hd a :: []), n - 1)
  15.         in podzielPoN_it(xs, [], n);;
  16. podzielPoN (xs, 2)
  17.  
  18.  
  19. let rec podzielWgMod = function (xs, n) ->
  20.        
  21.         let rec podzielWgMod_it = function (xs, a, b) ->
  22.                 if (xs = []) then (a, b)
  23.                 else if (List.hd xs mod n = 0) then podzielWgMod_it(List.tl xs, a @ (List.hd xs :: []), b)
  24.                 else podzielWgMod_it(List.tl xs, a, b @ (List.hd xs :: []))
  25.         in podzielWgMod_it(xs, [], []);;
  26. podzielWgMod(xs, 2);;
  27.  
  28.  
  29. let rec coNTy = function (xs, n) ->
  30.        
  31.         let rec coNTy_it = function (x, i) ->
  32.                 if (x = []) then []
  33.                 else if (i = 1) then List.hd x :: coNTy_it(List.tl x, n)
  34.                 else coNTy_it(List.tl x, i - 1)
  35.         in coNTy_it(xs, n);;
  36. coNTy(xs, 2);;