代写代考 11/24/2020

11/24/2020
Immutable Data Structure
What is an immutable data ?
Let Binding

Copyright By PowCoder代写 加微信 powcoder

Consider the Haskell expression where c1 is some constant.
let x=5 in x+c1
What result will be returned by the above expression under lazy evaluation?
Section 1 (Warm Up Questions)
It is a data that cannot be changed once it is set
It is a data that can be changed anytime.
It is a data that can be changed only once.
None of the Given Answers
It is a data that is never garbage collected.
None of the Given Answers

11/24/2020
What is purely functional programming? Choose the best answer below.
Let Binding
Consider the Haskell expression where c1 is some constant.
let x = c1 in
let f y = x+(y+y) in
f ((f x)+2)
How many additions will be performed by above code fragment under lazy evaluation?
None of the Given Answers
It is based on the concept of utilizing higher-order functions
It is based on defining functions without side-effects.
It is based on avoiding the use of global variables.
It is based on using functions with multiple inputs and multiple outputs.
None of the Given Answers

11/24/2020
Non-Termination
Consider the following recursive Haskell function.
if x=0 then y+y
else (foo (x-1) (y*2))
Does this function always terminates for all inputs, assuming the presence of infinite runtime resource?
Consider the following recursive Haskell function.
if x<=0 then y+y else (foo (x-1) (y*2)) What result will be returned by foo 3 c1, while c1 is some integer constant? Consider the following recursive Haskell function. Section 2 (MCQ - Choose One Answer only) None of the Given Answers 11/24/2020 Higher-Order Function Consider the following higher-order function in Haskell. gen_ho f xs n = case xs of y:ys -> (f y n):(gen_ho f xs (n+1))
What would be returned by the call (gen_ho f [1..] n) ?
if x<=0 then y+y else (foo (x-1) (y*2)) Which of the following is NOT correct about this method definition? This method has an accumulating parameter. None of the Given Answers This method terminates on all of its inputs, assuming unlimited computing resources. This method makes use of tail-recursion This method has a decreasing parameter which terminates the loop [f 1 1, f 2 2, .. , f n n] None of the Given Answers [f 1 1, f 2 2, .. ] [f 1 n, f 2 (n+1), .. ] [f 1 n, f 2 (n+1), .. , f n (n+n-1)] 11/24/2020 Consider the following Haskell expression let a = array (1,10) ((1,1):[(i, if mod i 2==0 then a!(i-1) else i) | i <- [1..10]]) What is the value of a!6 ? Consider the following Haskell expression let a = array (1,10) [(i, if mod i 2==0 then a!(i-1) else i) | i <- What is the value of a!6 ? None of the Given Answers None of the Given Answers 11/24/2020 List comprehension Consider the list comprehension [ g x y z | x in e1, y in e2, z in e3] Assume you have a filter condition (f x y) which you would like to add to the above list comprehension. Where would be the best place to put this filter? Consider the following recursive function in Haskell. foo g (x::Int) = g x (foo g x) What is the most general type that could be inferred for this function? immediately after generator (y in e2) immediately after generator (x in e1) immediately before generator (x in e1) None of the Given Answers immediately after generator (z in e3) (Int -> b) -> Int -> b
(Int -> Int -> Int) -> Int -> Int
None of the Given Answers
(a -> b -> b) -> a -> b

11/24/2020
Higher-Order Function
Consider the following simple higher-order function
foo xs g =
case xs of
[] -> (1,[])
let (c,cs)= foo ys g in
if g y then (1+c,y:cs)
else (c,cs)
What value is returned by the expression (fst (foo [-1,2,3,0] (\z -> z>0)) ?
Consider the following higher-order function in Haskell.
gen_ho f xs n =
case xs of
y:ys -> (f y n):(gen_ho f xs (n+1))
What would you do to this function definition to ensure that it will be correctly inferred by Haskell to have the type ((a->Int->b) -> [a] -> Int -> [b]) ?
(Int -> b -> b) -> Int -> b
None of the Given Answer
Add type annotation n::Int

11/24/2020
Higher-Order Functions
Consider the following higher-order function in Haskell.
gen_ho f xs n =
case xs of
y:ys -> (f y n):(gen_ho f xs (n+1))
What is computed by the expression (gen_ho f [c1,c2,c3] 1)?
List Comprehension
Consider the list comprehension:
[ (x,y) | x in e1, y in e2, z in e3]
Which of the statements below is NOT true?
Add type annotation xs::[Int]
Add type annotation f::a->Int->a
None of the Given Answers
Do Nothing
None of the Given Answers
[(f c3 1),(f c2 2),(f c1 3)]
[(f c1 0),(f c2 1),(f c3 2)]
f (f c1 1) (f (f c2 2) (f c3 3))
[(f c1 1),(f c2 2),(f c3 3)]

11/24/2020
Type Inference
Consider the following Haskell function.
foo g x = g x
Which of the following is the most general type of this function that would be inferred by Hindley-Milner type inference system used in the Haskell compiler?
Higher-Order Function
The generator (z in e3) has the effect of a filter condition.
You can add yet another copy of the generator (z in e3) without changing the functional correctness of the expression.
The generator (z in e3) is redundant since its absence does not affect the functional correctne of the expression.
None of the Given Answers
The generator (z in e3) can be re-positioned without changing the functional correctness of th expression.
(Int->Int) -> Int -> Int
None of the Given Answers
Num a => (a->a) -> a -> a
(a->a) -> a -> a
a -> (a -> a) -> a

11/24/2020
Consider the following recursive function in Haskell.
foo g (x::Int) = g x (foo g x)
What result would be returned by (foo (\ n r -> n+r) c1) where c1 is a symbolic constant.
Consider the following higher-order function in Haskell.
gen_ho f xs n =
case xs of
y:ys -> (f y n):(gen_ho f xs (n+1))
Which of the following higher-order function can be used to implement this gen_ho function using just a single call to the selected higher-order function ?
None of the Given Answers
None of the Given Answers

11/24/2020
Consider the following higher-order function in Haskell.
gen_ho f xs n =
case xs of
y:ys -> (f y n):(gen_ho f xs (n+1))
What would you do to this function definition to ensure that it will be correctly inferred by Haskell to have the type ((a->Int->a) -> [a] -> Int -> [a]) ?
Consider the following Haskell expression
let a = array (1,10) [(i, if mod i 2==0 then a!(i-1) else i) | i <- Which of the following is NOT true about this particular array? Add type annotation xs::[Int] Add type annotation f::a->Int->a
None of the Given Answers
Add type annotation n::Int
Do Nothing
It is a monolithic array that is built in one step.
It contains exactly 10 array entries
It will return an error due to absence of base case.
Its contents are lazily evaluated

11/24/2020
Section 3 (Multiple Response Answer)
This section contains questions with multiple response answers. You must provide all the answers to get 2 mark
Higher-Order Function
Consider the following function
foo xs g =
case xs of
Higher-Order Function
Consider the following function
foo xs g =
case xs of
[] -> (1,[])
let (c,cs)= foo ys g in
if g y then (1+c,y:cs)
else (c,cs)
One could re-implement the call (foo xs g) by an equivalent expression (foldl e2 e1 xs). Note that we are using foldl with he following type here:
foldl :: Foldable t => (b -> a -> b) -> b -> t a -> b
Which of the following is/are correct? This is a multiple response answer where all correct answer must be given.
None of the Given Answers
e1 = (1,[])
e2 = \ y (c,cs) -> if g y then (1+c,cs++[y]) else (c,cs)
e2 = \ y (c,cs) -> if g y then (1+c,y:cs) else (c,cs)

11/24/2020
[] -> (1,[])
let (c,cs)= foo ys g in
if g y then (1+c,y:cs)
else (c,cs)
One could re-implement the call (foo xs g) by an equivalent expression (foldr e1 e2 xs) which has the following type:
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
Which of the following is/are correct?
Section 4 (This section requires Answer Rationale)
Type Annotation
Consider the following Haskell function. foo g = (g 1, g “hello”)
Is a user annotated type possible for this function? Justify your answer by explaining why it is possible or otherwise.
e1 = \ y r -> if g y then (1+(fst r),tail r) else r
e2 = (1,[])
e1 = \ y (c,cs) -> if g y then (1+c,y:cs) else (c,cs)
None of the Given Answers
Response Rationale
Please provide a rationale for your answer.

11/24/2020
Consider the following recursive function in Haskell.
foo g (x::Int) = g x (foo g x)
It is not possible to implement the factorial function using this higher-order function since there isn’t a base case. Is this statement true or false.
Please qualify your answer.
Response Rationale
Please provide a rationale for your answer.

11/24/2020
Finish Quiz
Save For Later

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com