Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type 't tree =
- | Nil
- | Node of 't * 't tree * 't tree
- let Leaf x = Node(x, Nil, Nil)
- let t = Node(0, Leaf(1), Node(2, Leaf(3), Nil))
- let ex = Node('+',Leaf('1'),Node('*',Leaf('1'),Leaf('2')))
- type trav_order = Prefix|Infix|Postfix
- let rec traverse2 f = function
- | Nil -> ()
- | Node(x,l,r) -> f x
- traverse2 f l
- traverse2 f r
- let rec traverse tro f = function
- | Nil -> ()
- | Node(x,l,r) ->
- match tro with
- | Prefix -> f x; traverse tro f l; traverse tro f r
- | Infix -> traverse tro f l; f x; traverse tro f r
- | Postfix -> traverse tro f l; traverse tro f r; f x
- let rec fold f acc = function
- | Nil -> acc
- | Node(x,l,r)->
- let x1 = fold f acc 1
- let x2 = f x x1
- fold f x2 r
- fold (fun a b -> string(a) + b) "" ex
- let rec insert x = function
- | Nil -> Leaf x
- | Node(z,l,r) as N ->
- if x<z then Node(z, insert x l, r)
- elif x>z then Node(z, l, insert x r)
- else N
- let Rnd = new System.Random()
- let L = [for x in [1..10] -> Rnd.Next(1,100)]
- let flip f x y = f y x
- let curry f x y = f(x,y)
- L |> List.fold (flip insert) Nil |> fold (curry List.Cons) []
- let rec insert x = function
- | Nil -> Leaf (x,1)
- | Node((z,n),l,r) ->
- if x<z then Node((z,n), insert x l, r)
- elif x>z then Node((z,n), l, insert x r)
- else Node((x,n+1),l,r)
- open System.IO
- File.ReadAllLines(@"C:/Users/pavel/YandexDisk/programming/fsharp/text.txt")
- |> Array.
- |> Array.collect (fun s -> s.Split([|' ';'-';',';':';'.|]))
- |> Array.filter (fun x x -> x.Length>0)
- |> Array.fold (flip insert) Nil
- |> fold (curry List.Cons) []
- // |> List.sordBy (fun (w,f) -> -f)
- |> List.sordBy ((~-)<<snd)
- |> List.take 5
- 5 |> (+) 1 |> (*) 2 |> printfn "%d"
- let plus1 x f = f(x+1)
- let times2 x f = f(x*2)
- plus1 5 <| fun
- let rec len f = function
- | [] -> f(0)
- | _::t -> len()
- let rec size f = function
- | Nil -> f 0
- | Node(_l,r) ->
- l |> size (fun x1 ->
- r |> size (fun x2 -> f(1+x1+x2)))
- size id T
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement