Advertisement
biswasrohit20

f#1

May 16th, 2021 (edited)
1,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.33 KB | None | 0 0
  1. // Exercise 2.2 – Function Declaration I
  2. // Define a function addNum1 that adds 1 to a number
  3.  
  4. open System
  5.    
  6. let addNum1(x:int)=
  7.     printfn "%i" (x+1)
  8.        
  9. addNum1(10)
  10.  
  11. Console.ReadKey() |> ignore
  12.  
  13.  
  14.  
  15. // Define a function addNum10, which takes an integer argument, adds 10 to it, and returns the result.
  16.  
  17. open System
  18.    
  19. let addNum10(x:int):int=
  20.     x+10
  21.        
  22. printfn "%i" (addNum10(10))
  23.    
  24. Console.ReadKey() |> ignore
  25.  
  26.  
  27.  
  28.  
  29. // Define a function addNum20, which uses addNum10 to add 20 to a given integer
  30.  
  31.  
  32. open System
  33.    
  34. let addNum10(x : int) : int =
  35.     x + 10
  36.        
  37. let addNum20(y : int) : int =
  38.     (addNum10(addNum10(y)))
  39.    
  40. printfn "%i" (addNum20(10))
  41.  
  42.  
  43. Console.ReadKey() |> ignore
  44.  
  45.  
  46. // Exercise 2.3 – Function Declaration II
  47. // Define the function max2 that takes two integers as arguments and returns the largest of them
  48.  
  49. open System
  50.    
  51. let max2(x : int, y : int) : int =
  52.     if x>y then x
  53.     else y
  54.        
  55.    
  56. printfn "%i" (max2(50, 10))
  57.  
  58. Console.ReadKey() |> ignore
  59.  
  60.  
  61.  
  62. // Define a function evenOrOdd that takes an integer and prints out “even number”
  63. // if the given integer is even otherwise it prints out “odd number”.
  64.  
  65. open System
  66.  
  67. let evenOrOdd(x : int) =
  68.     if (x % 2)= 0 then printfn "even number"
  69.     else printfn "odd number"
  70.        
  71.    
  72. evenOrOdd(51)
  73.    
  74. Console.ReadKey() |> ignore
  75.  
  76.  
  77. // Define a function addXY that takes two integers and prints out the two integers before adding them.
  78.  
  79.  
  80. open System
  81.    
  82. let addXY(x : int, y : int)=
  83.     printfn "The sum of %i and %i is %i" x y (x+y)
  84.    
  85. addXY(50, 10)
  86.    
  87.  
  88. Console.ReadKey() |> ignore
  89.  
  90. // (Exercise 2.4 Optional)
  91. // Define a function addNum_j k that takes two integers j, k as arguments and
  92. // returns j + 10*k. For instance, addNum_jk 3 5 = 3 + 10*5 = 53. You are,
  93. // however, not allowed to use addition and multiplication directly: instead, you
  94. // must write a recursive solution that calls addNum10 defined in Exercise 02.03
  95. // above. (So addNum_jk 3 5 should be computed as 3 + 10 + 10 + 10 + 10 + 10.)
  96.  
  97.  
  98. open System
  99.  
  100. let addNum10(x:int):int=
  101.     x+10
  102.    
  103. let rec addNum_jk(j : int, k : int)=
  104.     let m = addNum10(j)
  105.     if( k = 1) then m
  106.     else (addNum_jk(m,k-1))
  107.    
  108. printfn "%i" (addNum_jk(3, 5))
  109.    
  110. Console.ReadKey() |> ignore
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement