Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 140115 uppgift 3
- let rec add arr =
- let len = Array.length arr
- if len = 1 then
- arr.[0]
- else
- add arr.[0..len/2-1] + add arr.[len/2..len-1]
- printfn "%f" (add [| 5.0;6.0;7.0;8.0 |])
- // 140115 uppgift 5
- let avg floatList =
- let sum = ref 0.0
- let rec foo (floatList : float list) =
- match floatList with
- | x::xs -> sum := !sum + x; foo xs
- | [] -> ()
- foo floatList
- printfn "%f" (!sum/float(floatList.Length))
- avg [2.2; 3.3; 4.4]
- // 140115 uppgift 6
- type tree<'a> = Node of tree<'a> * 'a * tree<'a> | Empty
- let rec getBågar tree =
- match tree with
- | Node(Empty, b, Empty) -> 0
- | Node(Empty, b, c) -> 1 + getBågar c
- | Node(a, b, Empty) -> 1 + getBågar a
- | Node(a, b, c) -> 2 + getBågar a + getBågar c
- | Empty -> 0
- printfn "%A" ([ 5.0;6.0;7.0;8.0] @ [1.0;2.0;3.0;4.0])
- // 140609 uppgift 1
- let rec opt_remove list =
- match list with
- | None::tail -> opt_remove tail
- | Some x::tail -> x::opt_remove tail
- | [] -> []
- //let opt_remove_v2 l =
- // l |> List.filter (fun x -> not (x = None)) |> List.map (fun (Some x) -> x)
- printfn "%A" (opt_remove [Some 17; None; None; Some 3; Some 0])
- // 140609 uppgift 3
- let rec dot arr1 arr2 =
- let len1 = Array.length arr1
- let len2 = Array.length arr2
- if len1 = 0 && len2 = 0 then
- 0
- else if len1 = len2 then
- arr1.[0] * arr2.[0] + dot (arr1.[1..]) (arr2.[1..])
- else
- failwith "u suk"
- printfn "%A" (dot [|1;2;3;4|] [|5;6;7;9|])
- // 140609 uppgift 5
- let printlist list =
- printf "[ "
- let rec printlistrec list =
- match list with
- | [] -> ()
- | [x] -> printf "%A" x
- | x::tail -> printf "%A; " x; printlistrec tail
- in printlistrec list
- printf " ]\n"
- printlist [1;2;3]
- // 140609 uppgift 6
- type sphereTree = Sphere of sphereTree * (float * float * float) * float * sphereTree | Empty
- let calcVolume radius = (4.0 * 3.14159265358979323846264338327950 * radius * radius * radius) / 3.0
- let rec calcTree tree =
- match tree with
- | Empty -> 0.0
- | Sphere(Empty, _, radius, Empty) -> calcVolume radius
- | Sphere(s1, _, radius, Empty) -> (calcTree s1) + (calcVolume radius)
- | Sphere(Empty, _, radius, s2) -> (calcTree s2) + (calcVolume radius)
- | Sphere (s1, _, radius, s2) -> (calcTree s1) + (calcVolume radius) + (calcTree s2)
- // 140820 uppgift 1
- let remove l1 l2 =
- List.filter (fun x -> not(List.exists (fun y -> x = y) l1)) l2
- printfn "%A" (remove [1;2;3;4;5;6] [4;5;6;7;8;9])
- System.Console.ReadKey() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment