Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Exercise 2.2 – Function Declaration I
- // Define a function addNum1 that adds 1 to a number
- open System
- let addNum1(x:int)=
- printfn "%i" (x+1)
- addNum1(10)
- Console.ReadKey() |> ignore
- // Define a function addNum10, which takes an integer argument, adds 10 to it, and returns the result.
- open System
- let addNum10(x:int):int=
- x+10
- printfn "%i" (addNum10(10))
- Console.ReadKey() |> ignore
- // Define a function addNum20, which uses addNum10 to add 20 to a given integer
- open System
- let addNum10(x : int) : int =
- x + 10
- let addNum20(y : int) : int =
- (addNum10(addNum10(y)))
- printfn "%i" (addNum20(10))
- Console.ReadKey() |> ignore
- // Exercise 2.3 – Function Declaration II
- // Define the function max2 that takes two integers as arguments and returns the largest of them
- open System
- let max2(x : int, y : int) : int =
- if x>y then x
- else y
- printfn "%i" (max2(50, 10))
- Console.ReadKey() |> ignore
- // Define a function evenOrOdd that takes an integer and prints out “even number”
- // if the given integer is even otherwise it prints out “odd number”.
- open System
- let evenOrOdd(x : int) =
- if (x % 2)= 0 then printfn "even number"
- else printfn "odd number"
- evenOrOdd(51)
- Console.ReadKey() |> ignore
- // Define a function addXY that takes two integers and prints out the two integers before adding them.
- open System
- let addXY(x : int, y : int)=
- printfn "The sum of %i and %i is %i" x y (x+y)
- addXY(50, 10)
- Console.ReadKey() |> ignore
- // (Exercise 2.4 Optional)
- // Define a function addNum_j k that takes two integers j, k as arguments and
- // returns j + 10*k. For instance, addNum_jk 3 5 = 3 + 10*5 = 53. You are,
- // however, not allowed to use addition and multiplication directly: instead, you
- // must write a recursive solution that calls addNum10 defined in Exercise 02.03
- // above. (So addNum_jk 3 5 should be computed as 3 + 10 + 10 + 10 + 10 + 10.)
- open System
- let addNum10(x:int):int=
- x+10
- let rec addNum_jk(j : int, k : int)=
- let m = addNum10(j)
- if( k = 1) then m
- else (addNum_jk(m,k-1))
- printfn "%i" (addNum_jk(3, 5))
- Console.ReadKey() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement