DavidNorgren

Untitled

Jun 4th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.62 KB | None | 0 0
  1.  
  2. // 140115 uppgift 3
  3. let rec add arr =
  4.     let len = Array.length arr
  5.     if len = 1 then
  6.         arr.[0]
  7.     else
  8.         add arr.[0..len/2-1] + add arr.[len/2..len-1]
  9.        
  10. printfn "%f" (add [| 5.0;6.0;7.0;8.0 |])
  11.  
  12. // 140115 uppgift 5
  13. let avg floatList =
  14.     let sum = ref 0.0
  15.  
  16.     let rec foo (floatList : float list) =
  17.         match floatList with
  18.         | x::xs -> sum := !sum + x; foo xs
  19.         | [] -> ()
  20.  
  21.     foo floatList
  22.     printfn "%f" (!sum/float(floatList.Length))
  23.    
  24. avg [2.2; 3.3; 4.4]
  25.  
  26. // 140115 uppgift 6
  27. type tree<'a> = Node of tree<'a> * 'a * tree<'a> | Empty
  28. let rec getBågar tree =
  29.     match tree with
  30.     | Node(Empty, b, Empty) -> 0
  31.     | Node(Empty, b, c) -> 1 + getBågar c
  32.     | Node(a, b, Empty) -> 1 + getBågar a
  33.     | Node(a, b, c) -> 2 + getBågar a + getBågar c
  34.     | Empty -> 0
  35.  
  36. printfn "%A" ([ 5.0;6.0;7.0;8.0] @ [1.0;2.0;3.0;4.0])
  37.  
  38. // 140609 uppgift 1
  39.  
  40. let rec opt_remove list =
  41.     match list with
  42.     | None::tail -> opt_remove tail
  43.     | Some x::tail -> x::opt_remove tail
  44.     | [] -> []
  45.    
  46. //let opt_remove_v2 l =
  47.  //   l |> List.filter (fun x -> not (x = None)) |> List.map (fun (Some x) -> x)
  48.  
  49. printfn "%A" (opt_remove [Some 17; None; None; Some 3; Some 0])
  50.  
  51. // 140609 uppgift 3
  52. let rec dot arr1 arr2 =
  53.     let len1 = Array.length arr1
  54.     let len2 = Array.length arr2
  55.     if len1 = 0 && len2 = 0 then
  56.         0
  57.     else if len1 = len2 then
  58.         arr1.[0] * arr2.[0] + dot (arr1.[1..]) (arr2.[1..])
  59.     else
  60.         failwith "u suk"
  61.  
  62. printfn "%A" (dot [|1;2;3;4|] [|5;6;7;9|])
  63.  
  64. // 140609 uppgift 5
  65. let printlist list =
  66.     printf "[ "
  67.     let rec printlistrec list =
  68.         match list with
  69.         | [] -> ()
  70.         | [x] -> printf "%A" x
  71.         | x::tail -> printf "%A; " x; printlistrec tail
  72.     in printlistrec list
  73.     printf " ]\n"
  74.  
  75. printlist [1;2;3]
  76.  
  77. // 140609 uppgift 6
  78. type sphereTree = Sphere of sphereTree * (float * float * float) * float * sphereTree | Empty
  79.  
  80. let calcVolume radius = (4.0 * 3.14159265358979323846264338327950 * radius * radius * radius) / 3.0
  81.  
  82. let rec calcTree tree =
  83.     match tree with
  84.     | Empty -> 0.0
  85.     | Sphere(Empty, _, radius, Empty) -> calcVolume radius
  86.     | Sphere(s1, _, radius, Empty) -> (calcTree s1) + (calcVolume radius)
  87.     | Sphere(Empty, _, radius, s2) -> (calcTree s2) + (calcVolume radius)
  88.     | Sphere (s1, _, radius, s2) -> (calcTree s1) + (calcVolume radius) + (calcTree s2)
  89.  
  90. // 140820 uppgift 1
  91. let remove l1 l2 =
  92.     List.filter (fun x -> not(List.exists (fun y -> x = y) l1)) l2
  93.  
  94. printfn "%A" (remove [1;2;3;4;5;6] [4;5;6;7;8;9])
  95.  
  96. System.Console.ReadKey() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment