Question
1. Functional Programming. Implement a function in OCAML that takes a number as an input and checks whether it is a prime number. The result
1. Functional Programming. Implement a function in OCAML that takes a number as an input and checks whether it is a prime number. The result should be a Boolean value.
2. Functional Programming. Implement the Fibonacci function by means of accumulative parameters so that it runs in linear time.
3. Functional Programming. Given the program:
let rec x = 1 and f = fun y x + y + z and z = 4 in let rec x = 2 and g = fun y x + (f y) + z in let rec x = 3 and f = fun y y and z = 5 in g 6 What is the output of the program a) with static scoping? b) with dynamic scoping?
4. Functional Programming. Implement the following functions in OCAML (alternatively in SML), without using corresponding libraries: member takes as arguments an element e and a list l and returns whether the element is included in the list. filter takes as arguments a predicate p and a list l and returns a list of the elements from l for which p is true. fold_left successively combines the elements of the list with some function starting from an initial value for the empty list. This means that: fold_left f e [x1; x2; . . . ; xn] = f (...( f (f ex1) x2) . . .) xn mapi takes as arguments a function f and a list l and applies f to each pair of an element from l and its position in l: mapi f [x1; x2; . . . ; xn] = [f x1 1; f x2 2; . . . ; f xn n] For f x i = x + i, the call mapi f [3; 3; 3] should return the list [4; 5; 6].
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started