Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
We define the identity function as a function that always return its parameter without any changes. Which of the following function is considered an identity function?
id = \x -> Just x
Higher Order Function
Copyright By PowCoder代写 加微信 powcoder
Question 2-4 uses the following code fragment in Haskell where the … is different for each question.
let x = 0 in
let thrice f = \x -> (f (f (f x))) in
Consider the following code fragment in Haskell.
let x = 0 in
let thrice f = \x -> (f (f (f x))) in (1mark) thrice (\y -> y + 1) x
What is the result of the above code fragment?
id = \y -> y + 0
id = \w -> (\z -> z) w
id = \w -> (\() -> w)
none of the other
1 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following code fragment in Haskell.
let x = 0 in
let thrice f = \x -> (f (f (f x))) in (1mark) thrice (thrice (\y -> y + 1)) x
What is the result of the above code fragment?
Consider the following code fragment in Haskell.
let x = 0 in
let thrice f = \x -> (f (f (f x))) in (1mark) thrice thrice (\y -> y + 1) x
What is the result of the above code fragment?
none of the other
none of the other
2 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following code fragment in Haskell.
let foo = let x = 0 in
let thrice f = \x -> (f (f (f x))) in (1mark) thrice
What is the most general type of foo above?
(a -> a) -> a -> a
Questions 6-10 uses the following recursive code in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
none of the other
Num a => (a -> a) -> a -> a
a -> a -> a -> a
a -> (a -> a) -> a
none of the other
3 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following recursive function in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
Whatistheresultofthefollowingfunctioncall(foo 9)? (1 mark)
Consider the following recursive function in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
How many distinct function calls to foo are made by the function call (foo 9)? For distinct calls, each set of calls with the same arguments are to be counted once. For instance, if (foo 100) calls (foo 50)twice,onlycountthiscallto(foo 50)once.
none of the other
4 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468...
Consider the following recursive function in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
How many total function calls to foo are made by the function call (foo 9)? For total calls, each set of calls are to be counted. For instance, if (foo 100) calls (foo 50) twice, only count this call to (foo 50) twice.
Consider the following recursive function in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
Whatistheresultofthefollowingfunctioncall(foo 1000)? (1 mark)
none of the above
none of the other
5 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468...
Consider the following recursive function in Haskell.
if x <= 0 then 1
else if x `mod` 2 == 0 then (foo (x `div` 2)) + (foo (x `div` 2))
else foo (x-1)
Whatdoesthefunctioncall(foo n)computesassumingpositiven? (1 mark)
The largest power of 2 smaller than n
none of the above
The smallest power of 2 larger than n
2 to the power of n
n to the power of 2
none of the above
6 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468...
Consider the following list-based function in Haskell.
foldm f init xs =
ping f init xs =
case xs of
[] -> init
(x:xs) -> f x (pong f init xs)
pong f init xs =
case xs of
[] -> init
(x:xs) -> f (ping f init xs) x
in ping f init xs
Whatvaluewillbereturnedbythecallfoldm (+) init [1,2,3,4,5]?
Consider the following list-based function in Haskell.
foldm f init xs =
ping f init xs =
case xs of
[] -> init
(x:xs) -> f x (pong f init xs)
pong f init xs =
case xs of
[] -> init
(x:xs) -> f (ping f init xs) x
in ping f init xs
Whatwillbetheequivalentorderofoperationofthecallfoldm (+) init [1,2,3,4,5]?
none of the other
7 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following list-based function in Haskell.
foldm f init xs =
ping f init xs =
case xs of
[] -> init
(x:xs) -> f x (pong f init xs)
pong f init xs =
case xs of
[] -> init
(x:xs) -> f (ping f init xs) x
in ping f init xs
Whatvaluewillbereturnedbythecallfoldm (-) init [1,2,3,4,5]?
((2 + ((4 + (0 + 5)) + 3)) + 1)
(1 + (2 + (3 + (4 + (5 + 0)))))
(((((0 + 1) + 2) + 3) + 4) + 5)
(1 + ((3 + ((5 + 0) + 4)) + 2))
none of the other
none of the other
8 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following list-based function in Haskell.
foldm f init xs =
ping f init xs =
case xs of
[] -> init
(x:xs) -> f x (pong f init xs) (1 mark) pong f init xs =
case xs of
[] -> init
(x:xs) -> f (ping f init xs) x
in ping f init xs
What is the most general type of foldm?
Thefactorialofnisdefinedasn! = n * (n-1) * (n-2) * (n-3) * … * 3 * 2 * 1. Whichofthefollowingcodedoesnotcomputefactorialofnassumingthatn > 0?
(a -> b -> b) -> b -> a -> b
fact n = foldl (*) 1 (take (n-1) [2..])
(b -> a -> b) -> b -> a -> b
(a -> a -> a) -> a -> a -> a
(a -> b -> b) -> a -> b -> b
none of the other
fact = (\n -> foldr (*) 1 (take (n-1) [2..]))
9 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
The following section asks about lazy evaluation and pattern matching.
Consider the following code fragment in Haskell.
let f xs = case xs of
[] -> [1]
(x:xs) -> x:(x*2):xs (1 mark) let x = [1] ++ (f x) in
What is the result of the code fragment above?
[1,2,1,2,1,2]
fact n = let aux m n =
_ -> aux (m*n) (n-1)
in aux 1 n
aux m 1 = m
aux m n = aux (m*n) (n-1) fact1 =1
factn =aux1n
none of the other(i.e.,allothercomputesfactorial)
[1,1,2,1,2,1]
[1,1,1,1,1,1]
[1,2,2,2,2,2]
none of the other
10 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following Haskell function.
case xs of
[] -> []
(x:xs) -> [(x,x)] ++ (foo xs)
Whatistheresultoffoo [1,2,3]?
Consider the following Haskell function.
case xs of
[] -> []
(x:xs) -> [(x,x)] ++ (foo xs)
[1,1,2,2,3,3]
Which of the following higher-order function could not be used to reimplement the function foo? (1 mark)
[1,2,3,1,2,3]
[(1,1),(2,2),(3,3)]
[(3,3),(2,2),(1,1)]
none of the other
11 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following Haskell function.
case xs of
[] -> []
(x:xs) -> [(x,x)] ++ (foo xs)
What is the most general type of foo?
a -> (a,a)
Questions 20-21 uses the following type
Algebraic Data Type
data Term = Var String | Fun String Term | App Term Term
deriving (Eq, Show)
none of the other(i.e.,allothercanbeusedtoimplementfoo)
[a] -> [(a,a)]
a -> [(a,a)]
[a] -> [a]
none of the other
12 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
You are given the following algebraic data type:
data Term = Var String | Fun String Term | App Term Term
deriving (Eq, Show)
This data type is a representation of lambda calculus. We wish to pretty print the term such that the following term:
term = App (Fun “y” (Var “y”)) (Var “x”)
willbeprintedasfollowswhenweinvoke(pprint term):
“((^y y) x)”
Which of the following code fragment can be used to define pprint in declarative style such that it will return the above string? Choose the most correct choice.
pprint (Var x) = x
pprint (Fun x t) = “(^” ++ x ++ ” ” ++ (pprint t) ++ “)”
pprint (App f v) = “(” ++ (pprint f) ++ ” ” ++ (pprint v) ++ “)”
At least two of the other choices(exceptnoneoftheother)
none of the other(i.e.,noneofthechoicesprintscorrectly)
13 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
You are given the following algebraic data type:
data Term = Var String | Fun String Term | App Term Term
deriving (Eq, Show)
This data type is a representation of lambda calculus. We wish to pretty print the term such that the following term:
term = App (Fun “y” (Var “y”)) (Var “x”) willbeprintedasfollowswhenweinvoke(pprint term):
“((^y y) x)”
Assume(pprint term)returnsString,whatisthemostgeneraltypeofpprint?
Term -> [Char]
This section is about Haskell monad
Var -> String
Fun -> String
App -> String
none of the other
14 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Recap that the Maybe monad can be defined as follows: data Maybe a = Nothing | Just a
Recapthatthefunction>>=hasthetypesignatureof:Monad m => m a -> (a -> m b) -> m b
Consider the following operation on a Maybe monad:
op x = if x == 1 then Nothing
else if x `mod` 2 == 0 then Just (x `div` 2)
else Just ((3 * x) + 1)
rep f init 1 = init
rep f init n = rep f (init >>= f) (n-1)
What is the result of rep (op) (Just 7) 3? Note that this is simply (Just 7) >>= op >>= op >>= op.
none of the other
15 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Recap that the Maybe monad can be defined as follows: data Maybe a = Nothing | Just a
Recapthatthefunction>>=hasthetypesignatureof:Monad m => m a -> (a -> m b) -> m b
Consider the following operation on a Maybe monad:
op x = if x == 1 then Nothing
else if x `mod` 2 == 0 then Just (x `div` 2)
else Just ((3 * x) + 1)
rep f init 1 = init
rep f init n = rep f (init >>= f) (n-1)
Consider the function call rep (op) (Just 5) n. What is the smallest value of n such that the result of the function call above is Nothing?
Consider the following code fragment in Haskell.
xs = [print “this”, print “is”, print “TIC”, print “2701”] (1 mark)
foldr (>>) (print ” “) xs
What is printed by the foldr on the code fragment above?
“this” “is” “TIC” “2701” “”
none of the other
16 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
Consider the following code fragment in Haskell.
xs = [print “this”, print “is”, print “TIC”, print “2701”] (1 mark)
foldl (>>) (print ” “) xs
What is the equivalent code fragment corresponding to the foldl above?
print “this”
print “is”
print “TIC”
print “2701”
“” “this” “is” “TIC” “2701”
“2701” “TIC” “is” “this” “”
“” “2701” “TIC” “is” “this”
none of the other
print “this”
print “is”
print “TIC”
print “2701”
17 of 18 6/10/2020, 11:07 pm
Firefox https://luminus.nus.edu.sg/modules/16e753cc-c01b-4d6a-9fa7-526468…
print “2701”
print “TIC”
print “is”
print “this”
print “2701”
print “TIC”
print “is”
print “this”
none of the other
18 of 18 6/10/2020, 11:07 pm
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com