Advertisement
kamasazi99

f#

Nov 15th, 2019
1,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.46 KB | None | 0 0
  1. let obwod a b=a+2*b
  2. obwod 1 2
  3. let pole_trojkata (a:float) (b:float)= 0.5*a*b
  4. pole_trojkata 1.0 2.0
  5.  
  6.  
  7. //zad2
  8.  
  9. let x=3
  10. let rec suma_mnozenia a =
  11.     if a < 1 then 0
  12.     else x + suma_mnozenia (a - 1)
  13. suma_mnozenia 4
  14.  
  15. //zad 3
  16. let x=2
  17. let rec mnozenie_mnozenia a =
  18.     if a < 1 then 1
  19.     else x * mnozenie_mnozenia (a - 1)
  20. mnozenie_mnozenia 3
  21.  
  22. //zad4
  23. let rec e_s_l list buff bb =
  24.   match list with
  25.   | x::r -> if bb=true then (e_s_l r (x::buff) false) else (e_s_l r buff true)
  26.   | [] -> List.rev buff
  27. printf "exc.4 %A" (e_s_l [1;2;3;4;5;6;7;8;9] [] true)
  28. //zad5
  29.  
  30. let rec_hr list =
  31.   let bb = true
  32.   list |> List.indexed|> List.filter(fun  (i, x)  -> i%2=0 ) |> List.map (fun (i, x) -> (x))
  33. printf "exc.5 %A" (rec_hr [1;2;3;4;5;6;7;8;9] )
  34.  
  35.  
  36. //zad6
  37. let rec filter_by_n list buff n =
  38.   match list with
  39.   | x::r -> if x%n=0 then (filter_by_n r (x::buff) n) else (filter_by_n r buff n)
  40.   | [] -> List.rev buff
  41. printf "exc.6 %A" (filter_by_n [1;2;3;4;5;6;7;8;9] [] 3)
  42.  
  43. //zad7
  44.  
  45. let rec_hr list n = List.filter(fun x -> x%n=0 ) list
  46. printf "exc.7 %A" (rec_hr [1;2;3;4;5;6;7;8;9] 3)
  47.  
  48. //zad 8
  49. type Ksiazka={
  50.     Tytul : string
  51.     Autor : string
  52.     Rok_wydania : int
  53.     Wydawnictwo : string
  54.     liczba_stron : int
  55.     Twarda_oprawa : bool
  56.     Miekka_oprawa : bool
  57.     }
  58.  
  59.     //9-
  60. let min_max_rec list =
  61.   let rec min list got =
  62.      match list with
  63.     | x::rest -> if x<got then min rest x else min rest got
  64.     | [] -> got
  65.   let rec max list got =
  66.     match list with
  67.     | x::rest -> if x>got then max rest x else max rest got
  68.     | [] -> got
  69.   (min list list.[0], max list list.[0])
  70. printfn "ex.9 %A" (min_max_rec [3;2;1;5;8;2])
  71. //zad10
  72. let min_max list = (List.min list, List.max list)
  73. printfn "ex.10 %A" (min_max [3;2;1;5;8;2])
  74.  
  75. //11-
  76. let average_rec list =
  77.   let rec sum list got=
  78.     match list with
  79.     |x::rest -> sum rest (x + got)
  80.     |[] -> got
  81.  
  82.   (sum list 0) / (List.length list)
  83.  
  84. printfn "ex 11 %A" (average_rec [1;2;3;4])
  85.  
  86. //12-
  87. let average_high list =
  88.   let sum list = List.fold (+) 0 list
  89.   (float (sum list))/(float (List.length list))
  90. printfn "ex 12 %A" (average_high [1;2;3;4])
  91.  
  92. //13-
  93. let rec count_less list n count =
  94.   match list with
  95.   | x::rest ->if x<n then count_less rest n (count+1) else count_less rest n count
  96.   |[] -> count
  97. printfn "exc.13 : %A" (count_less [2;3;7;11;4;9] 5 0)
  98.  
  99. //14-
  100. let less_n list n = list |> List.filter(fun x-> x>=n) |> List.length
  101. printfn "ex.14 : %A" (less_n [2;3;7;11;4;9] 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement