haskell Flashcards
3.0 + 4
7.0
8.0 / 2
4.0
negation
not True
or
False || True
and
&&
not equal
/=
4 ways to concatenate. Hint: add, colon, colon tuple, colon all the way
[1,2,3] ++ [4,5] ⇒ [1,2,3,4,5]
[] ++ [1,2] ⇒ [1,2]
3 : [4,5] [3,4,5]
2 : (3 : (4 : [])) [2,3,4]
2 : 3 : 4 : [] [2,3,4]
whats the type for ‘abc’
errro
whats the output for ‘a’ ++ “bcd”
error
write two ways to add a list of characters to a string.
hint: concat operator and colon.
“ab” ++ [‘c’,’d’]
compare list of chars with a string. tell me whats the output
[‘X’, ‘Y’] == “XY” True
write me a function that takes a and b and returns a list of a and b
listify a b = [a, b]
write a two line recursie mylength function
myLength [] = 0
myLength (_:xs) = 1 + myLength xs
write a are two lists equal recursive function considering edge cases
listEqual [] [] = True
listEqual (x:xs) (y:ys) = x == y && listEqual xs ys
listEqual [] (:) = False
listEqual (:) [] = False
(or listEqual _ _ = False and use the first-match-only behaviour.)
write a even odd length function:
two ways:
- recursively
- using the even
function
hasEvenLength [] = True – base case: length 0 is even
hasEvenLength [ _ ] = False – base case: length 1 is odd
hasEvenLength (_ : _ :rest) = hasEvenLength rest
or:
hasEvenLength xs = even (length xs)
what are the 3 Rules for pattern matching:
plain identifier (argument name, like x): matches anything and captures the value.
_: matches anything but we don’t need the value.
literal value (like 1, [], etc): matches if equal.
what are the 4 ways to match lists
hint:
match 0 elements
match 1,
match 2,
or match all of the rest of the elements
[]: matches an empty list.
[a]: matches a one-element list.
(a:tail): matches a list with at least one element. First is a, rest of list is tail (and could be an empty list).
(a:b:tail): matches a list with at least two elements. First is a, second is b, remainder of list is tail (could be empty).
if we have (a:b:c), what is the result for the following arguments:
a b c
[6, 7, 8, 9]
[‘a’,’b’]
“ab”
[100]
6, 7, [8,9]
‘a’ ‘b’ []
‘a’ ‘b’ [] (==””)
[100]: ERROR
write a simple mySigNum function using conditional
mySignum x
| x>0 = 1
| x<0 = -1
| otherwise = 0
write using a case expression that matches 1, 2, 3 with their words, everything else with ????. and finally, concatenate it with “X”
wordWithX n = (case n of
1 -> “one”
2 -> “two”
3 -> “three”
_ -> “???”) ++ “X”
write the describe llist function. concatenate “the list is “ with corresponding list length
describeList lst = “The list is “ ++ case lst of
_ : _ : _ : _ : _ -> “fairly long” – >= 4 elements
_ : _ -> “short” – >= 1 element
[] -> “empty”
write a list comprehension for x^2+1 with x = 1 to 7
[ x^2+1 | x <- [1, 2, 3, 4, 5, 6, 7] ]
use more than one generator to fwrite 10*x + y
[ 10*x + y | x <- [3,4,5], y <- [6,7,8,9]]
create a list by using the even guard x in x^2+1
[ x^2+1 | x <- [1, 2, 3, 4, 5, 6, 7], even x ]
build a list function
n = [ i*i | i <- [2,4..n]]
write a quick sort example with where clause
qs [] = []
qs (x:xs) = smaller ++ [x] ++ larger
where smaller = qs [a | a<-xs, a<=x]
larger = qs [a | a<-xs, a>x]
write a quick sort example in one line with list comprehension
qs (x:xs) = qs [a | a <- xs, a <= x] ++ [x] ++ qs [a | a <- xs, a > x]
write quicksort it with let clause:
BASE CASE
qs’ [] = []
qs’ (x:xs) =
let smaller = qs’ [a | a<-xs, a<=x]
larger = qs’ [a | a<-xs, a>x]
in smaller ++ [x] ++ larger
write a simple myPower recursive function
myPower _ 0 = 1
myPower x y = x * myPower x (y-1)
write an OlogN myPower’ using conditional
myPower’ x y
| y==0 = 1
| even y = halfhalf
| odd y = xhalf*half
where half = myPower’ x (div y 2)
whats the flag to optimize haskell code
ghc -O2
write tail recursive my power
myPowerTailRec a _ 0 = a
myPowerTailRec a x y = myPowerTailRec (x*a) x (y-1)
write complete tail recursive factorial function with where clause
factorial’ n = factorialTailRec 1 n
where
factorialTailRec a 0 = a
factorialTailRec a n = factorialTailRec (n*a) (n-1)
what overhead does tail recursion avoid
overhead of maintaining the call stack
write a function that takes n nums from list, use split function
split :: Int -> [Int]
myTake n xs: start
where (start, _) = split n xs
write a myGCD function
myGCD 0 b = b
myGCD a 0 = a
myGCD a b = gcd b r
Where (q,r) = a divMod b
filter even numbers from a list using the even function and filter
Filter even [1..8]
filter even numbers from a list using list comprehension
[ n | n <- [1..8], even n]
map sqrt to a list
map sqrt [1..8]
map sqrt to a list with list comprehension
[ sqrt n | n <- [1..8]]