Advertisement
pavelperc

very bad

Oct 2nd, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. type 't tree =
  2. | Nil
  3. | Node of 't * 't tree * 't tree
  4.  
  5. let Leaf x = Node(x, Nil, Nil)
  6.  
  7. let t = Node(0, Leaf(1), Node(2, Leaf(3), Nil))
  8.  
  9. let ex = Node('+',Leaf('1'),Node('*',Leaf('1'),Leaf('2')))
  10.  
  11. type trav_order = Prefix|Infix|Postfix
  12.  
  13. let rec traverse2 f = function
  14. | Nil -> ()
  15. | Node(x,l,r) -> f x
  16.  
  17. traverse2 f l
  18. traverse2 f r
  19.  
  20.  
  21. let rec traverse tro f = function
  22. | Nil -> ()
  23. | Node(x,l,r) ->
  24. match tro with
  25. | Prefix -> f x; traverse tro f l; traverse tro f r
  26. | Infix -> traverse tro f l; f x; traverse tro f r
  27. | Postfix -> traverse tro f l; traverse tro f r; f x
  28.  
  29. let rec fold f acc = function
  30. | Nil -> acc
  31. | Node(x,l,r)->
  32. let x1 = fold f acc 1
  33. let x2 = f x x1
  34. fold f x2 r
  35.  
  36. fold (fun a b -> string(a) + b) "" ex
  37.  
  38. let rec insert x = function
  39. | Nil -> Leaf x
  40. | Node(z,l,r) as N ->
  41. if x<z then Node(z, insert x l, r)
  42. elif x>z then Node(z, l, insert x r)
  43. else N
  44.  
  45. let Rnd = new System.Random()
  46.  
  47. let L = [for x in [1..10] -> Rnd.Next(1,100)]
  48.  
  49. let flip f x y = f y x
  50. let curry f x y = f(x,y)
  51. L |> List.fold (flip insert) Nil |> fold (curry List.Cons) []
  52.  
  53.  
  54.  
  55. let rec insert x = function
  56. | Nil -> Leaf (x,1)
  57. | Node((z,n),l,r) ->
  58. if x<z then Node((z,n), insert x l, r)
  59. elif x>z then Node((z,n), l, insert x r)
  60. else Node((x,n+1),l,r)
  61.  
  62. open System.IO
  63. File.ReadAllLines(@"C:/Users/pavel/YandexDisk/programming/fsharp/text.txt")
  64. |> Array.
  65. |> Array.collect (fun s -> s.Split([|' ';'-';',';':';'.|]))
  66. |> Array.filter (fun x x -> x.Length>0)
  67. |> Array.fold (flip insert) Nil
  68. |> fold (curry List.Cons) []
  69. // |> List.sordBy (fun (w,f) -> -f)
  70. |> List.sordBy ((~-)<<snd)
  71. |> List.take 5
  72.  
  73.  
  74. 5 |> (+) 1 |> (*) 2 |> printfn "%d"
  75.  
  76. let plus1 x f = f(x+1)
  77. let times2 x f = f(x*2)
  78.  
  79. plus1 5 <| fun
  80.  
  81. let rec len f = function
  82. | [] -> f(0)
  83. | _::t -> len()
  84.  
  85.  
  86. let rec size f = function
  87. | Nil -> f 0
  88. | Node(_l,r) ->
  89. l |> size (fun x1 ->
  90. r |> size (fun x2 -> f(1+x1+x2)))
  91.  
  92. size id T
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement