Haskell Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

let

A

Defines a function

let original = 2
let add x y = x + y

Other way:
let a = 1 in a + a
let a = 2; b = 4 in a * b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

map

A

Map is a name of a higher-order function that applies a given function to each element of the list, returning a list of results.

let number = [2, 4 , 6]
map (+3) numbers = [5, 7, 9]

map (\x -> x*x) [1..5]
map function list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Filter

A

Filter is the name of a higher-order function that processes a data structure, typically a list, in some order to produce a new data structure containing those exact elements of the original data structure that match a given condition.

let numbers = [25,9,(-20),100,16,(-2)]
filter (<16) numbers = shows numbers below 16

filter f (x:xs)
|f x = x : rest
|otherwise = rest
where rest = filter f xs

square_even :: [Int] -> [Int]
square_even list = map (^2) (filter even list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Fold

A

Reduce/Fold is the name of a higher-order function which reduces a list of values to a single value by repeatedly applying a combining function to the list values.

numbers = [2, 4, 6]
foldl (+) 0 numbers = adds all numbers including 0 from left to right.

sum’’ list = foldr (\ x acc -> acc + x) 0 list

Acc:
Actuator -> acc = 0 , then becomes 0 + whatever is on the right of the list

foldr1 = uses the last element in the list as the accumulator
foldr1 cannot change the type of the list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Side Effect

A

A side effects is anything that changes global state. This means that is could change a variable, a console etc, in which pure functions disallow things like prints, loops and variables. No control flow either, like if statements, because that makes the result indeterministic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Pure Function

A

A functions with NO SIDE EFFECTS, meaning it is DETERMINISTIC and ALWAYS RETURNS A VALUE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Subroutine

A

They predominately have side effects. These subroutines that have side effects cause issues for code refactoring, complier designers, optimization and parrelization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

**

A

Float (square)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A

Integer (square)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

&&
||

A

AND statement
OR statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Not

A

Gives opposite for boolean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

==

A

Equality testing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

/=

A

Not equal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Negatives

A

Must be in brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Priority

A

Functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

mod

A

Remainder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

``

A

Allows function to become infix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Brackets

A

Allows add and sub to become prefix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Succ

A

Successor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

::

A

Has type
:t True
True :: Bool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

– and {–}

A

Allows comments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Show

A

Turn anything into a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

putStrLn

A

Prints

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

If

A

If True then 1 else 0, both branches must exist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Lists

A

Contain items that all have the same type.
[]
They are linked (go through everything from the beginning)
Last, Init, Head, Tail
: = puts new head on (1: [2,3,4,5])
!! = index of list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Tuples

A

Allows us to bind two or move values together
Can contain different types
()
Fixed length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Range

A

[1..10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Step Size

A

[2,4..20]
Would give = [2,4,6,8,10 etc]
Counting downwards always needs a step size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

take 4 [1..]

A

Infinite list, but returns the first four.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Repeat and Cycle

A

Repeats the input and repeats the list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

List Comprehension

A

Can produce more complex lists:
[ 2*x |x <- [1,2,3,4]] = [2,4,6,8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Predicates

A

[2*x | x <- [1,2,3,4], x<2] = [2]
Using commas to filter results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

List Comprehension : Upper Case

A

st [c | c <- st, c ‘elem’ [‘A’…‘Z’]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

List Comprehension : Prime Numbers With Factorials

A

factors n = [x | x <- [1..n], n mod x == 0]

prime n = [x | x <- [1..n], length (factors x) == 2] (only got 2 factors)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Recursion

A

factorial n = if n > 1
then n * factorial (n-1)
else 1
Base case - not making recursive rule (else 1)

Another way:
factorial 1 = 1
factorial n = n * factorial (n-1)

36
Q

Guards

A

Guards:
f x
| x < 1 = “strictly less”
| x > 1 = “strictly more”
| otherwise = “other”
n == 1 = 1 - used for guards (format)

37
Q

Recursion With Lists

A

sum’ [] = 0 - base case
sum’ (x:xs) = x + sum’ xs

38
Q

Where

A

f x = x + a
where a = x + 1
Defines a
Difference between where and let is that let can be used multiple times.

39
Q

Zip’

A

zip’ takes two lists and returns a list of pairs
zip [1..10]”hello there”
[1,h][2,e] etc.

40
Q

Mutual Recursion

A

even’ 0 = True
even’ n = odd’ (n-1)
odd’ = False
odd’ n = even’ (n-1)
Recursive methods that call each other.

41
Q

Multiple Recursion

A

fib 0 = 0
fib 1=1
fib n = fib (n-1) + fib (n-2)
Multiple recursions in one line.

42
Q

Quicksort

A

Sorts elements in a list.
pivot element p
lower = all elements smaller than p
upper = all element larger than p
return sorted_lower ++ [p] ++ sorted_upper
split pivot [] = ([],[])
split pivot (x;xs) etc etc. (x lower or higher)
qs [] = []
qs (x:xs) = qs lower ++ [x] ++ qs upper
where (lower,upper) = split x xs

43
Q

:t

A

Will display the type
:t True = True :: Bool

is_lower c = c elem [a…z]
:t is_lower = char -> bool

44
Q

Int
Integer
Double

A

Holds 64-bit but faster
Holds arbitrary but slower
Holds 64-bit float

45
Q

Partial Application

A

plus a b = a+b
alternative:
plus2 = plus 1
plus2 2 = 3
Also known as currying

46
Q

Polymorphism

A

:t length
length :: [a] -> int
Works on all lists that have different types.
[function name] :: [type]

47
Q

fst

A

Returns first element of two ; tuple

48
Q

snd

A

Returns second element of two ; tuple

49
Q

:t take

A

take :: Int -> [a] -> [a]

50
Q

Difference between : and ++

A

++ takes two lists, : takes a single value

51
Q

:info …

A

Tells information on function.

52
Q

:t (+)

A

(+) = Num a => a -> a -> a
if you give two a’s which are equal to Num a, then return a

53
Q

f (x, y) = (x+1, y==2)
:t f

A

(eq a1, num a2, num a1) => ((the inputs)a2,a1) -> ((the results)a2, bool)

54
Q

General Type Annotations

A

The most general type annotation is the one that is least restrictive:
equals_two a b = a + b == 2
equals_two :: (Eq a, Num a) => a -> a -> bool

55
Q

fromIntegeral

A

Convert back to a more generic type.

56
Q

Show and Read

A

Show = converts other types into strings
Read = converts strings into other types

57
Q

x = [take 1, length] -> why does this not work?

A

Doesn’t work since they are different types ; take returns a list, length returns an integer

58
Q

Dot Operator

A

((+1) . (*2)) 4 -> dot operator
Use of dot operator removes the need for nested brackets.

59
Q

$

A

Used to evaluate the input.

60
Q

Anonymous Function

A

(\ x -> x+1) 5 -> this is an anonymous function; a function that has no name. This is meant to represent lambda.

61
Q

Scan

A

Like Fold, but prints the accumulator each time:

scanr (+) 0 [1,2,3,4]
[10,9,7,4,0]

scan1 (+) [1..10]
[0,1,3,6,10,15,21,28,36,45,55]

62
Q

Takewhile

A

Takes from a list while a condition is true

takeWhile (<=5) [1..10]
[1,2,3,4,5]

63
Q

Dropwhile

A

dropWhile drops from a list while a condition is true

dropWhile (==1) [1,1,2,2,3,3]
[2,2,3,3]

64
Q

Zipwith

A

Zips two lists using a function

zipWith (+) [1,2,3] [4,5,6]
[5,7,9]

65
Q

Type

A

type String’ = [char]
… :: String’ -> String’

type VoteResults = [(Int, String)]
results :: VoteResults
results = [(1, “Red”), …]

66
Q

Data

A

data Bool’ = True | False

data Direction = North | East | South | West deriving Show
Adds Direction to show, since Direction is not part of a class at the beginning
Show, Read, Eq, Ord = all derivable : deriving (Show, Read, Eq, Ord)

data TwoThings = Things Int Char deriving Show
f (Things x _ ) = x
f (Things 2 ‘a’) = 2

= or

67
Q

Record

A

Record Type:
data Person = Person String String Int String
get_first_name (Person x _ _ _) = x etc…
Record Type 2:
data Blah = Blah { one :: Int, two :: Char } deriving Show

68
Q

Difference between pattern matching lists and tuples

A

Lists = (s:s)
Tuples = (s,s)

69
Q

Use foldr to write a function product of evens that takes a list of
numbers, and multiplies all the even elements together.
Use foldr to write a function lt10 that takes a list of numbers and
returns the number of elements that are strictly less than 10.

You HAVE to use acc.

A

product_of_evens list = foldr (\ x acc -> if even x then x * acc else
acc) 1 list

lt10 list = foldr (\ x acc -> if x < 10 then 1 + acc else acc) 0 list

70
Q

Maybe, Just, and Nothing

A

data Maybe a = Just a | Nothing
safe_head [] = Nothing
safe_head (x:_) = Just x

71
Q

Case

A

h = safe_head list
case h of Just x -> x
Nothing -> 0

f x = case x of (x:xs) -> x
[] -> 0

72
Q

Either’

A

data Either’ a b = Left a | Right B

ghci> let list = [Left “one”, Right 2,
Left “three”, Right 4]

is_left (Left _) = True
is_left _ = False

73
Q

map unleft unright list *

A

Cannot be done as trying to combine two lists that contain different values will lead to an error.

74
Q

Recursive Data Types

A

data IntList = Empty | Cons Int IntList deriving(Show)
Cons 4 ( Cons 5 ( Cons 6 Empty)))

data a = Empty | Cons a (List a) deriving(Show)
Cons “hi” Empty

data TwoList a b = TwoEmpty
|ACons a (TwoList a b)
|BCons b (TwoList a b)
deriving(Show)

75
Q

Tree Search with Recursive Data Types

A

data TwoList a b = TwoEmpty
|ACons a (TwoList a b)
|BCons b (TwoList a b)
deriving(Show)

data Tree = Leaf | Branch Tree Tree deriving(Show)
size :: Tree -> Int
size (Leaf) = 1
size (Branch x y) = 1 + size x + size y

data DTree = DLeaf a | DBranch a (DTree a) (DTree a) deriving(Show)
tree_sum :: Num a => DTree a -> a
tree_sum (DLeaf x) = x
tree_sum (DBranch x l r) = x + tree_sum l + tree_sum r

76
Q

getLine

A

Reads an input line from the console (input) ; requires an IO String
:t -> getLine :: IO String

77
Q

putStrLn

A

Takes a string and prints out hi (print) ; requires a pure string
:t :: String -> IO ()

78
Q

getChar

A

Gives back a char from input char

79
Q

Conc two strings together via two inputs

A

print_two :: String -> String -> IO ()
print_two s1 s2 = putStrLn (s1 ++ s2)

80
Q

do / do blocks

A

Allows sequence of actions followed by an IO ; only works with IO.
get_and_print :: IO ()
get_and_print =
do
x <- getLine
y <- getLine
putStrLn (x ++ “ “ ++ y)

81
Q

return

A

Takes a pure value and returns the value in an IO box ; used for “do nothing”.
Return, in functional programming, does NOT stop the program running.
print_if_short :: String -> IO ()
print_if_short str =
if length str <= 2
then putStrLn str
else return ()
:: Monad m => a -> m a

82
Q

Mutual Recursion

A

Two functions which call upon one another.

83
Q

Multiple Recursion

A

Makes more than one recursive call in the actual function line.

84
Q

Tail Recursion

A

Nothing left to do once the function has made the recursive call.

85
Q

Lazy Evaluation

A
86
Q

List recursion

A

A recursion using lists.