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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!