Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.30 KB | None | 0 0
  1. let length list =
  2.     let rec loop l ac =
  3.         match l with
  4.         | head :: tail -> loop tail (ac + 1)
  5.         | [] -> ac
  6.     loop list 0
  7.  
  8.  
  9. let mapr f list =
  10.     let rec loop l ac =
  11.         match l with
  12.         | head :: tail -> loop tail (ac @ [f head])
  13.         | [] -> ac
  14.     loop list []
  15.  
  16.  
  17. let maxl list =
  18.     if list = [] then
  19.         raise (System.ArgumentException())
  20.     else
  21.         let rec loop ac = function
  22.             | head :: tail -> loop (max head ac) tail
  23.             | [] -> ac
  24.         let h::t = list
  25.         loop h t
  26.  
  27.  
  28. let minli list =
  29.     if list = [] then
  30.         raise (System.ArgumentException())
  31.     else
  32.         let rec loop l ac i ai =
  33.             match l with
  34.             | head :: tail ->
  35.                 let t = min head ac
  36.                 if t <> ac then loop tail t (i + 1) i
  37.                 else loop tail t (i + 1) ai
  38.             | [] -> ai
  39.         let h::t = list
  40.         loop t h 0 0
  41.  
  42.  
  43. let rec append l1 l2 =
  44.     match l1 with
  45.     | head :: tail -> head :: (append tail l2)
  46.     | [] -> l2
  47.  
  48. let rec appendr list = function
  49.     | head :: tail -> appendr (list @ [head]) tail
  50.     | [] -> list
  51.  
  52. let primes n =    
  53.     let rec loop list i =
  54.         match i < n with
  55.         | true -> loop [for x in list do if x = i || x % i <> 0 then yield x] (i + 1)
  56.         | false -> list
  57.     loop [2..n] 2
  58.        
  59.  
  60. let listse one two =
  61.     let rec loop list1 list2 outlist =
  62.         match list1 with
  63.         | h::t ->
  64.             match list2 with
  65.             | [] -> outlist @ list1
  66.             | h2::t2 -> loop t t2 (outlist @ [h] @ [h2])
  67.         | [] -> outlist @ list2
  68.     loop one two []
  69.  
  70.  
  71. let nconcat list =
  72.     let rec loop l outlist =
  73.         match l with
  74.         | h::t -> loop t (outlist @ h)
  75.         | [] -> outlist
  76.     loop list []
  77.  
  78.  
  79. [<EntryPoint>]
  80. let main argv =
  81.     printfn "%d" (length [1; 2; 3])
  82.     printfn "%A" (map ((+) 1) [1; 2; 3])
  83.     printfn "%A" (mapr ((+) 2) [1; 2; 3])
  84.     printfn "%d" (maxl [1; 3; 2])
  85.     printfn "%d" (maxli [1; 3; 2])
  86.     printfn "%A" (append [1; 2; 3] [4; 5; 6])
  87.     printfn "%A" (appendr [1; 2; 3] [4; 5; 6])
  88.     printfn "%A" (primes 20)
  89.     printfn "%A" (listse [1; 2; 3] [4; 5; 6])
  90.     printfn "%A" (nconcat [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]])
  91.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement