Advertisement
pavelperc

sem3

Sep 18th, 2018
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.83 KB | None | 0 0
  1. open System.Xml.Xsl
  2.  
  3. //
  4. //let rec ins x = function
  5. //| [] -> [[x]] // x = 0
  6. //| y::ys as l-> // y=1  ys= 2 3
  7. //    let L = ins x ys // list of lists
  8. //    let N = List.map(fun z -> y::z) L
  9. //    (x::l)::N
  10. //
  11. //ins 0 [1;2;3]
  12.  
  13. // constructor
  14. let cons x y = x::y
  15.  
  16. let rec ins x = function
  17. | [] -> [[x]] // x = 0
  18. | y::ys as l-> // y=1  ys= 2 3
  19.     (x::l)::(ins x ys |> List.map (cons y))
  20. ins 0 [1;2;3]
  21.  
  22. //let rec perm = function
  23. //| [] -> [[]]
  24. //| x::xs -> // x = 1 xs = [2 3]
  25. //    let L = perm xs// [[2;3];[3;2]]
  26. //    let M = List.map (fun t -> ins x t) L // [ [[1;2;3];[2;1;3];[2;3;1]] ; [[]] ; [[]]]
  27. //    List.concat M
  28.  
  29. let rec perm = function
  30. | [] -> [[]]
  31. | x::xs -> // x = 1 xs = [2 3]
  32.     perm xs
  33.     |> List.collect (ins x)
  34.  
  35.  
  36.  
  37. perm [1..3]
  38.  
  39. perm ["Hi";"there";"my";"friends"]
  40.  
  41. let fact n = [1..n] |> perm |> List.length
  42. fact 5
  43.  
  44. //
  45. //let rec fold f i = function
  46. //| [] -> i
  47. //| x::xs -> f x (fold f i xs)
  48. //
  49. //fold (fun x acc -> "f(" + x + "," + acc + ")") "empty" [1;2;3]
  50.  
  51.  
  52.  
  53. let rec foldr f i = function
  54. | [] -> i
  55. | x::xs -> f x (foldr f i xs)
  56.  
  57. foldr (fun x acc -> "f(" + string(x) + "," + acc + ")") "empty" [1;2;3]
  58.  
  59. // it is tail recursive for some reason...
  60. let foldl f  i L =
  61.     let rec fold' acc f = function
  62.    | [] -> acc
  63.    | x::xs -> fold' (f x acc) f xs
  64.     fold' i f L
  65.  
  66. foldl (fun x acc -> "f(" + string(x) + "," + acc + ")") "empty" [1;2;3]
  67.  
  68. //// O(n^2)
  69. //let rec rev = function
  70. //| [] -> []
  71. //| x::xs -> (rev xs)@[x]
  72. //
  73. //rev[1..100]
  74.  
  75.  
  76. //// tail recursive
  77. //let rev L =
  78. //    let rec rev' acc = function
  79. //    | [] -> acc
  80. //    | x::xs -> rev' (x::acc) xs
  81. //    rev' [] L
  82. //    
  83. //rev[1..100]
  84.  
  85. let curry f x y = f(x,y)
  86.  
  87. let rev L = foldl (curry List.Cons) [] L
  88.  
  89. rev [1..100]
  90.  
  91. foldl (-) 0 [1;2;3;4]
  92. foldr (-) 0 [1;2;3;4]
  93.  
  94. // array
  95. let A = [|1;2;3|]
  96. A.[0]
  97. A.[0] <- 2
  98. A
  99.  
  100. [1..2..10] // step 2
  101. [for x in [1..10] -> x*2]// map
  102. [for x in [1..10] do yield x*2]// the same
  103.  
  104. [for x in [1..10] do
  105.  for y in [1..10] do
  106.  if (x+y)=5 then yield (x,y)]
  107.  
  108.  
  109. // my variant of pascal triangle:
  110.  
  111. let next L =
  112.     let l1 = L |> List.pairwise |> List.map (fun (a,b) -> a + b )
  113.     1::l1@[1]
  114.  
  115. next [1;3;3;1]
  116.  
  117. let pasc n =
  118.     let rec pascrev (n:int): list<list<int>> =
  119.         match n with
  120.         | 1 -> [[1;1]]
  121.         | _ -> match pascrev (n - 1) with
  122.                | x::xs as prev-> (next x)::prev
  123.     pascrev n |> List.rev
  124.  
  125. pasc 5
  126.  
  127.  
  128. // functional queue is made as two stacks 1 2 3 4 = ([1;2], [4;3])
  129.  
  130. type 't queue = 't list * 't list
  131. let put x (L,M) = (L,x::M)
  132.  
  133. let rec get = function
  134. | ([],[]) -> failwith "Oh no!"
  135. | ([],L) -> get (rev L, [])
  136. | (x::xs,L) -> (x,(xs,L))
  137.  
  138.  
  139. let empty = ([],[])
  140.  
  141. //    let fst (x,y) = x
  142. //    let snd (x,y) = y
  143.  
  144. empty |> put 5 |> put 4 |> get |> snd |> put 1 |> get |> snd |> get
  145.    
  146.    
  147. // zipper: ([3,4], [1,2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement