Using F# write, compile and test the following simple programs. You should check
ID: 3918929 • Letter: U
Question
Using F# write, compile and test the following simple programs. You should check the examples and programs we worked on in class using the dotnetfiddlecommand line tool.
Try to avoid using imperative constructs as much as possible.
1. Implement an add function. Run the following tests. a. add 25 15 b. add 18 34 23 19 2. Write a program that uses loop. Compute the average of a list of integers. Use List comprehensions to generate the List. List comprehension refers to special syntactic constructs (ex: ranges and generators) used for generating lists 3. Write a function round that takes an integer argument and implements the following expression: if x ?= 100 return 100 eïif x ? 0 return 0 else x Write a program that implements the expression using a. if-then-else, and then using b. Pattern matching Test the program for different values of x to test the various conditionsExplanation / Answer
Q1. let add x y = x + y //(for first example)
let add w x y z = w+ x+ y+ z//(for second example)
Q2.
let mutable sum = 0
let arrX= [|9; 11; 3; 4; 5; 6; 7; 8|]
for i in 0 .. arrX.Length - 1 do
sum <- sum + arrX.[i]
printfn "-> avg is %f" (float sum / float arrX.Length)
Q3.
let round x =
if x >= 100 then 100
elif x < 0 then 0
else x
printfn "%d" (round 5)
Q4.
let rec factorial n =
if n = 0
then 1
else n * factorial (n - 1)
System.Console.WriteLine(factorial 3)
Q5. It prints the current date and time in the local system.
All the best
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.