Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(5 pts) 3. Consider the following code. What does it do? put adequate meaningful

ID: 3693216 • Letter: #

Question

(5 pts) 3.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality.

                                                            f1 p [] = []

                                                            f1 p (x:xs)

                                                                        | p x = x : f1 p xs

                                                                        | otherwise = []

(5 pts) 4.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality.

                                                                        f1 1 p s = [i | i <- s, p i]

(5 pts) 5.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality.

                                                            f2 p f x

                                                                      | p x = x

                                                                        | otherwise = f2 p f (f x)

(5 pts) 6.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality.

                                                f3 f (a:as) (b:bs) = f a b : f3 f as bs

                                                f3 _ _ _ = []

(5 pts) 7.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality. Note f3 is from question 6.

                                                f4 xs ys

                                                            = f3 op xs ys

                                                                      where

                                                                                  op x y = (x, y)

(5 pts) 8.         Consider the following code. What does it do? put adequate meaningful comments on each line. For comments don’t explain the code, rather say its overall functionality.

                                                            f7 x = foldr (++) [] x

Explanation / Answer

3)
f1 p [] = [] ----> array declared
f1 p (x:xs) ----> accessing each array
| p x = x : f1 p xs -----> copying each element into array
| otherwise = []

----------------------------------------------------------------------------------

4)
f1 1 p s = [i | i <- s, p i] ------> accessing each value in array and printing value

---------------------------------------------------------------------------------------------
5)
f2 p f x
| p x = x -----> checking value is equal or not
| otherwise = f2 p f (f x) -----> if not equal calling recursively

--------------------------------------------------------------------------------------
6)
f3 f (a:as) (b:bs) = f a b : f3 f as bs -----> multiplying both elements from two lists and
f3 _ _ _ = []

------------------------------------------------------------------------------------------
7)
f4 xs ys
= f3 op xs ys
where
op x y = (x, y) -----------------> mapping two list values parametres into new map list

---------------------------------------------------------------------------------------------------------------
8)
f7 x = foldr (++)[] x --------------> incrementing the values in list x