1/24
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Type of ['a','b','c'] :: ?
[Char] - a list with Chars; elements of a list must be of the same type.
Type of ('a','b','c') :: ?
(Char, Char, Char) - a tuple with three components, where each component is Char. A tuple can have different types, so it encodes the number of components as well.
Type of [(False, '0'),(True, '1')] :: ?
[(Bool, Char)] - a list with tuples, each tuple is type (Bool, Char).
Type of ([False,True],['0','1']) :: ?
([Bool],[Char]) - a tuple with two components, one a list of Bools and the other a list of Chars.
Type of [tail, init, reverse] :: ?
[[a] -> [a]] - A list of functions, where each function is a list handling function of type [a] -> [a].
write `min` using if conditional
min x y = if x <= y then x else y
write `min` using guards
min x y
| x <= y = x
| otherwise = y
[1,[2,3],4] is NOT a valid Haskell list
The elements of a list must all be the same.
Given the Haskell function definition
f n = n * n
what is the result of:
f 2 + 1
(f 2) + 1
(2 * 2) + 1 = 5
What is the result of the following Haskell expression:
takes 3 [3..10] !! 2
takes 3 [3..10] = [3,4,5]
[3,4,5] !! 2 = 5
(!! means get at index of)
The Haskell expression `take 3` has type:
[a]->[a] -
THESE ALL ARE TRUE:
In Haskell, ALL type checking and inference is done at runtime.
Haskell's pureness is what helps guarantee referential transparency.
Haskell functions are NOT all total functions.
Miranda is a lazy functional programming language developed before Haskell, so Haskell CANNOT be the first lazy functional programming language
Given the following definitions:
f x = 3
g x = g (x-1)
what is f (g 0)
3 - Since f of any x is 3, we don't even evaluate the argument `g 0` because of Haskell's laziness.
What is the result of
(head . tail) [1..]
head (tail [1..]) = head [2..] = 2
What is the type of the following expression: [([]++)]
[[a] -> [a]] - the given expression is a list of a section of the append (++) operator with its left operand list ([]) already provided. The type of section ([] ++) is [a] -> [a] since it needs the right operand list as the argument to put the two lists together, which will be the final result.
Which of the following statements about Haskell `foldr` is false?
foldr is a curried function
foldr is a recursive function
foldr is a higher-order function
foldr is a polymorphic function
foldr is an overloaded function
foldr is an overloaded function.
foldr is a curried function - it takes more than one argument possibly at a time.
foldr is a recursive function - its definition involves recursion.
foldr is a higher-order function - it takes function as an argument.
foldr is a polymorphic function - its definition involves type parameters that are instantiated per application.
foldr is an overloaded function - it does NOT involve any overloaded operations that require type constraints.
What is the type of the following Haskell expression? "Hi,":"all!":[]
[[Char]]
Haskell's ___ is what helps guarantee referential transparency.
pureness
Haskell's __ is what helps avoid unnecessary computation and ensures that programs terminate whenever possible
laziness
Haskell's pureness prohibits or confines
side-effects
Consider a function `safetail :: [a] -> [a]` that behaves in the same way as the library function `tail`, except safetail maps the empty list to the empty list, whereas tail gives an error in this case. Define safetail using
a) a conditional expression
b) guarded equations
c) argument pattern matching
a)
safetail xs = if null xs then [] else tail xs
b)
safetail xs
| null xs = []
| otherwise = tail xs
c)
safetail [] = []
safetail xs = tail xs
fz xs = sum [ x | (z,x) <- zip [1..] xs, z==x]
what is fz [1..4]
sum [ x | (z,x) <- zip [1..] [1..4], z==x] = sum [1,2,3,4] = 10
product $ map (div 3) [1,2,3]
= product (map (div 3) [1,2,3])
= product [(div 3 1), (div 3 2), (div 3 3)]
= product [3,1,1]
= 3
fun p = foldr (\x xs -> if p x then x + xs else xs) 0
what is fun (> 0) [-1, -2, 0, 1]
= foldr (\x xs -> if (>0) x then x + xs else xs) 0 [-1,-2,0,1]
let f = (\x xs -> if (>0) x then x + xs else xs)
= foldr f 0 ((:) -1 ((:) -2 ((:) 0 ((:) 1 []))))
= f -1 (f -2 (f 0 (f 1 0))) -- substitute [] with 0 and each (:) with f
= f -1 (f -2 (f 0 ( if 1 > 0 then 1 + 0 else 0)))
= f -1 (f -2 ( if 0 > 0 then 0 + 1 else 1))
= f -1 (if -2 > 0 then -2 + 1 else 1)
= if -1 > 0 then -1 + 1 else 1
= 1
Given the following grammar in BNF