Advertisement
stevensoekha

Week 1 - Functional Programming

Jun 1st, 2020
2,838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 3.50 KB | None | 0 0
  1. module Program
  2.  
  3. open System
  4.  
  5. let inc = fun x -> x + 1
  6. let abs = fun x -> if x >= 6 then x else -x
  7.  
  8. let rec addTo =
  9.     fun n ->
  10.         if n = 0 then
  11.             0
  12.         else
  13.             n + (addTo (n - 1))
  14.  
  15. //! There is another syntax for writing addTo
  16. let rec addToAlt n =
  17.     if n = 0 then
  18.         0
  19.     else
  20.         n + (addTo (n - 1))
  21.  
  22. // Part 4 -  Making an operator, mind the order in math * before +
  23. let (>==) = fun x y -> x + y * x
  24.  
  25. //TODO: Exercise 1
  26. let rec allNumber n =
  27.     if n = 0 then
  28.         "0"
  29.     else
  30.         (allNumber (n - 1)) + " " + (string n)
  31.  
  32. // TODO: Exercise 2
  33. let rec allNumberRev n =
  34.     if n = 0 then
  35.         "0"
  36.     else
  37.         (string n) + " " + (allNumberRev (n - 1))
  38.  
  39. // TODO: Exercise 3
  40. let rec allNumberRange l h =
  41.     if l = h + 1 then
  42.         ""
  43.     else
  44.         (string l) + " " + (allNumberRange (l + 1) h)
  45.  
  46. // TODO: Exercise 4
  47. let rec allNumberRangeRev l h =
  48.     if l = h then
  49.         (string l)
  50.     else
  51.         (allNumberRangeRev (l + 1) h) + " " + (string l)
  52.  
  53. // TODO: Exercise 5
  54. let rec allEvenRange lower upper =
  55.     if lower = upper then
  56.         if lower % 2 = 0 then
  57.             (string lower)
  58.         else
  59.             ""
  60.     else
  61.         let current =
  62.             if lower % 2 = 0 then
  63.                 (string lower)
  64.             else
  65.                 ""
  66.         current + " " + (allEvenRange (lower + 1) upper)
  67.  
  68. // TODO: Exercise 6
  69. let rec drawLine n =
  70.     if n = 0 then
  71.         ""
  72.     else
  73.         (drawLine (n - 1)) + "*"
  74.  
  75. // TODO: Exercise 7
  76. let rec drawSymbols char length =
  77.     if length = 0 then
  78.         ""
  79.     else
  80.         (drawSymbols char (length - 1)) + char
  81.  
  82. // TODO: Exercise 8
  83. let rec toBinary n =
  84.     if n < 0 then
  85.         "Incorrect parameter."
  86.     else
  87.         if n = 0 then
  88.             ""
  89.         else
  90.             (toBinary (n / 2)) + (string (n % 2))
  91.  
  92. // TODO: Exercise 9
  93. //! Don't understand the question..
  94.  
  95. [<EntryPoint>]
  96. let main argv =
  97.     printfn "Running the program.."
  98.  
  99.     //* Part 1
  100.     let resBig = (fun x -> x + 1) 1
  101.     // Same as
  102.     let res = inc 1
  103.     printfn "%d" resBig
  104.     printfn "%d" res
  105.  
  106.     //* Part 2
  107.     let num = abs 5
  108.     let num2 = abs -10
  109.     printfn "abs func %d and %d" num num2
  110.  
  111.     //* Part 3
  112.     let add = addTo 5
  113.     let addalt = addToAlt 10
  114.     printfn "the addTo func %d and the addToAlt func %d" add addalt
  115.  
  116.     //* Part 4 - Operator
  117.     let op = 5 >== 3
  118.     printfn "My result is %d" op
  119.  
  120.     printfn "\n\n----Exercises ------------------------\n\n"
  121.  
  122.     let exercise n =
  123.         printfn "Exercise %d" n
  124.  
  125.     //TODO: Exercise 1
  126.     exercise 1
  127.     let num = allNumber 10
  128.     printfn "My result is %s" num
  129.     printfn "\n"
  130.  
  131.     //TODO: Exercise 2
  132.     exercise 2
  133.     let num = allNumberRev 10
  134.     printfn "My result is %s" num
  135.     printfn "\n"
  136.  
  137.     //TODO: Exercise 3
  138.     exercise 3
  139.     let num = allNumberRange 16 23
  140.     printfn "My result is %s" num
  141.     printfn "\n"
  142.  
  143.     //TODO: Exercise 4
  144.     exercise 4
  145.     let num = allNumberRangeRev 16 23
  146.     printfn "My result is %s" num
  147.     printfn "\n"
  148.  
  149.     //TODO: Exercise 5
  150.     exercise 5
  151.     let num = allEvenRange 4 10
  152.     printfn "My result is %s" num
  153.     printfn "\n"
  154.  
  155.     //TODO: Exercise 6
  156.     exercise 6
  157.     let line = drawLine 10
  158.     printfn "My result is %s" line
  159.     printfn "\n"
  160.  
  161.     //TODO: Exercise 7
  162.     exercise 7
  163.     let line = drawSymbols "&" 5
  164.     printfn "My result is %s" line
  165.     printfn "\n"
  166.  
  167.     //TODO: Exercise 8
  168.     exercise 8
  169.     let num = toBinary 34
  170.     printfn "My result is %s" num
  171.     printfn "\n"
  172.  
  173.     //TODO: Exercise 9
  174.     //! Don't understand the question..
  175.  
  176.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement