Skip to content

Commit

Permalink
Merge pull request #1 from RoRu/hw02
Browse files Browse the repository at this point in the history
Complete second homework
  • Loading branch information
RoRu committed Mar 27, 2015
2 parents 793db94 + 34550d9 commit 7ba994d
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
52 changes: 52 additions & 0 deletions list_func/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Solution for functions of List via List.fold
// by Yumatov Vladimir
// SPBSU 171 gr.

// Expected execution time: 2 h
// Real time: 3.5 h

// Task 9
// List.iter : ('a -> unit) -> 'a list -> unit

// Task 10
let Reverse ls = List.fold (fun res elm -> elm::res) [] ls


// Task 11
let Filter f ls = List.foldBack (fun elm res ->
if f(elm) then elm::res else res) ls []


// Task 12
let Map f ls = List.foldBack (fun elm res -> f(elm)::res) ls []


// Task 13 Horner's method
let Horner a cfs = List.fold (fun res elm -> elm + res * a) 0 cfs


[<EntryPoint>]
let main argv =
printfn "Task 9 - List.iter : ('a -> unit) -> 'a list -> unit\n"

let ls = [1; 2; 3; 4; 5; 6]

let n = Reverse ls
printfn "Task 10 - Reversed list %A is %A\n" ls n

let fil z =
z % 2 = 0
let n = Filter fil ls
printfn "Task 11 - Filter: Even elements of %A are %A\n" ls n

let Dbl z =
z * 2
let n = Map Dbl ls
printfn "Task 12 - Map: Doubled elements of %A are %A\n" ls n


let cfs = [4; 5; 9; 1] // 4x^3 + 5x^2 + 9x + 1
let x0 = 5
let n = Horner x0 cfs
printfn "Task 13 - Horner's result with coefficients %A in x=%d is %d\n" cfs x0 n
0
155 changes: 155 additions & 0 deletions poly_binsearch_tree/binsearch_tree.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Task 14
// Solution for polymorphic binary search tree
// by Vladimir Yumatov
// SPBSU 171 gr.

// Expected execution time: 2.5 h
// Real time: ~3.5 h


// Type for Binary search tree
type BsTree<'A> =
| Empty
| Item of 'A * BsTree<'A> * BsTree<'A>

// Add element x to tree t
let rec BstAdd x t =
match x, t with
| x, Empty -> Item(x, Empty, Empty)
| x, Item(y, lft, rgt) ->
match (compare x y) with
| c when c > 0 -> Item(y, lft, BstAdd x rgt)
| c when c < 0 -> Item(y, BstAdd x lft, rgt)
| _ -> t

// Delete element x from tree t
let rec BstDel x t =
match x, t with
| _, Empty -> Empty
| _, Item(y, lft, rgt) ->
match (compare x y) with
| c when c > 0 -> Item(y, lft, BstDel x rgt)
| c when c < 0 -> Item(y, BstDel x lft, rgt)
| _ ->
match lft, rgt with
| Empty, _ -> rgt
| _, Empty -> lft
| _, Item(y', Empty, rgt') -> Item(y', lft, rgt')
| _, Item(y', lft', rgt') ->
let rec ReplaceSearch t =
match t with
| Empty -> y'
| Item(n, Empty, _) -> n
| Item(_, l, _) -> ReplaceSearch l

let rep = ReplaceSearch lft'
Item(rep, lft, BstDel rep (Item(y', lft', rgt')))

// Print tree t
let rec BstPrint opt t =
match t with
| Empty -> ()
| Item(x, lft, rgt) ->
match opt with
| "LCR" ->
BstPrint opt lft
printf "%A " x
BstPrint opt rgt
| "LRC" ->
BstPrint opt lft
BstPrint opt rgt
printf "%A " x
| "CLR" ->
printf "%A " x
BstPrint opt lft
BstPrint opt rgt
| _ -> printf "%s " "Unknown command\n"



// Task 15 Map for tree
let rec TreeMap f tr =
match tr with
| Empty -> Empty
| Item(x, lft, rgt) -> Item(f x, TreeMap f lft, TreeMap f rgt)


// Task 16 Fold for tree
let rec TreeFold f res tr =
match tr with
| Empty -> res
| Item(x, lft, rgt) -> TreeFold f (TreeFold f (f res x) lft) rgt
//f (f (TreeFold f res lft) (TreeFold f res rgt)) x


// Task 17 Sum of elements in integer tree
let TreeSum tr = TreeFold (fun a b -> a + b) 0 tr


// Task 18 Find min value in tree
let TreeMin tr =
let rec LessElm a b = // Returns the least element out of a, b
match a with
| None -> Some b
| Some a -> Some (min a b)
TreeFold LessElm None tr


// Task 19 Copy tree
let TreeCopy tr = TreeFold (fun res t -> BstAdd t res) Empty tr


[<EntryPoint>]
let main argv =
let mutable ts = Empty
BstPrint "LCR" ts
printf "\n"

ts <- BstAdd "x" ts
ts <- BstAdd "z" ts
ts <- BstAdd "y" ts
ts <- BstAdd "d" ts
ts <- BstAdd "l" ts

printfn "Task 14 - Tree with stings:"
BstPrint "LCR" ts
printf "\n"

ts <- BstDel "x" ts
ts <- BstDel "l" ts

printfn "\nSame tsee with two deleted elements:"
BstPrint "LCR" ts
printf "\n"

let mutable tr = Empty

tr <- BstAdd 4 tr
tr <- BstAdd 2 tr
tr <- BstAdd 1 tr
tr <- BstAdd 3 tr
tr <- BstAdd 9 tr
tr <- BstAdd 10 tr

printfn "\nTest tree:"
BstPrint "LCR" tr


let n = TreeMap (fun z -> z * 2) tr
printfn "\n\nTask 15 (Map) - Doubled elements of tree:"
BstPrint "LCR" n;

let n = TreeFold (fun a b -> if (b % 2 = 0) then a + b else a) 0 tr
printfn "\n\nTask 16 (Fold) - Sum of even elements:\n2+4+10 = %d" n

let n = TreeSum tr
printfn "\nTask 17 - Sum of elements:\n4+2+1+3+9+10 = %d\n" n

let n = TreeMin tr
printfn "Task 18 - The least element is %A\n" n

let n = TreeCopy tr
printfn "Task 19 - Copying of tree:"
BstPrint "LCR" n
printfn "\n"
0

0 comments on commit 7ba994d

Please sign in to comment.