Advertisement
biswasrohit20

f#3

May 16th, 2021
1,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.88 KB | None | 0 0
  1. // Exercise 3.2 – Some List functions
  2. // a. Define a function pmLength ls that computes the length of a list. Use pattern matching.
  3. // For example, pmLength [‘x’; ‘y’; ‘z’ ] should return 3.
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. // b. Define a function pclSum ls that sums all the numbers in a list. For example, pclSum [2; 3; 5; 8 ] should return 18.
  17.  
  18.  
  19. open System
  20.  
  21.  let pclSum (ls) : int=
  22.     List.reduce(+) ls
  23.  
  24.  printfn "%i" (pclSum([1;2;3;4;5]))
  25.  
  26. Console.ReadKey() |> ignore
  27.  
  28.  
  29.  
  30. // Exercise 3.3 – Lists
  31. // Define a function, takeSome n ls that returns list of first n elements from the list ls.
  32. // Define the function using pattern matching:
  33. // Example takeSome 2 ['a';'b'; 'c'; 'd'] should return ['a'; 'b'].
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. // Exercise 4.1 – List functions
  46. // a. Define the function pclFold . Use pattern matching. You are not allowed to use the standard F# functions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement