Advertisement
biswasrohit20

f# Ex 5

May 19th, 2021
1,830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.45 KB | None | 0 0
  1. // Exercise 5.1
  2. // Write an F# function countNumOfVowels to count the number of vowels in a given string. The type is:
  3.  
  4.  
  5. open System  
  6.    
  7. let  countNumOfVowels (s:string)=
  8.  
  9.     let isVowel (c:char) =
  10.         if (c = 'a' || c = 'A') then true
  11.         else if (c = 'e' || c = 'E') then true
  12.         else if (c = 'i' || c = 'I') then true
  13.         else if (c = 'o' || c = 'O') then true
  14.         else if (c = 'u' || c = 'U') then true
  15.         else false
  16.    
  17.     let mutable count = 0
  18.     for x in s do
  19.         if (isVowel(x)) then count <- count + 1
  20.     count
  21.  
  22. printfn "%i" (countNumOfVowels("Higher-order functions can take and return functions of any order"))
  23.  
  24. Console.ReadKey() |> ignore
  25.  
  26.  
  27.  
  28. // Exercise 5.2
  29. // Define a function primesUpTo n to create a list of prime numbers up to a given
  30. // number. For instance: primesUpTo 10 results in [2; 3; 5; 7]
  31.  
  32.  
  33. open System  
  34.    
  35. let  primesUpTo (n:int)=
  36.  
  37.     let isPrime (k:int) =
  38.         let mutable prime = true
  39.         for i in [2..k-1] do
  40.             if k % i = 0 then prime <- false
  41.         prime
  42.    
  43.     let list = [for i in [2..n] do if isPrime(i) = true then yield i]
  44.     list
  45.  
  46. printfn "%A" (primesUpTo(10))
  47.  
  48. Console.ReadKey() |> ignore
  49.  
  50.  
  51.  
  52. // Exercise 5.3
  53. // Define an F# function pclFib n that, when given a number, returns the nth Fibonacci number.
  54.  
  55. open System
  56.  
  57. let rec fib (n:int) =
  58.     if n = 1  then 0
  59.     else if n = 2 || n = 3 then 1
  60.     else fib (n - 1) + fib (n - 2)
  61.  
  62.  
  63. printfn "%i" (fib(10))
  64.  
  65. Console.ReadKey() |> ignore
  66.  
  67.  
  68.  
  69. // Exercise 5.4
  70. // a. Define two F# functions doubleNum x that multiplies x by 2 and sqrNum x that multiplies x by itself.
  71.  
  72. open System
  73.  
  74. let doubleNum (x) = x * 2
  75. let sqrNum (x) = x*x
  76.  
  77. printfn "Result for double num for %i is %i" 10 (doubleNum(10))
  78. printfn "Result for sqrare num for %i is %i" 10 (sqrNum(10))
  79.  
  80. Console.ReadKey() |> ignore
  81.  
  82.  
  83.  
  84. // b. Define another F# function pclQuad x that applies the doubleNum function defined above twice.
  85.  
  86. open System
  87.  
  88. let doubleNum (x) = x * 2
  89. let pclQuad (x) = (doubleNum(doubleNum(x)))
  90.  
  91. printfn "Result for pclQuad for %i is %i" 10 (pclQuad(10))
  92.  
  93. Console.ReadKey() |> ignore
  94.  
  95. // c. Define another F# function pclFourth x that applies the sqrNum function defined (in a.) above twice.
  96.  
  97. open System
  98.  
  99. let sqrNum (x) = x*x
  100. let pclFourth (x) = (sqrNum(sqrNum(x)))
  101.  
  102. printfn "Result for pclFourth for %i is %i" 10 (pclFourth(10))
  103.  
  104. Console.ReadKey() |> ignore
  105.  
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement