Higher Order Functions Flashcards

1
Q

what does map do?

A

applies a function to every element in a list.

map (+1) [ 1, 3, 5, 7]
[2, 4, 6, 8]

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

what does filter do?

A

selects every element in a list that satisfies a predicate

filter :: (a -> Bool) -> [a] -> [a]

filter even [1..10]
[2, 4, 6, 8, 10]

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

what is the pattern of recursion?

A

f[] = v

f(x : xs) = x ⊕ f xs

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

give a recursive definition of sum and its foldr version

A

recursive:
sum[] = 0
sum(x : xs) = x + sum xs

foldr:
sum = foldr (+) 0

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

give a recursive definition of and and its foldr version

A

recursive:
and[] = True
and (x : xs) = x && and xs

foldr:
and = foldr (&&) True

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

give the foldr definition of length

A

length = foldr (_ n -> 1 + n) 0

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

describe the difference between intesionality and extensionality in a sentence

A

\x.(1+x) is intensionally different from \x.(x+1) but extensionally equal

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

what does . do?

A

returns the composition of 2 funstions as a single function

.) :: (b -> c) -> (a -> b) -> (a -> c
f.g = \x -> f(g x)

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

use . to define odd

A

odd :: Int -> Bool

odd = not.even

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

what does all do?

A

check whether every element satisfies a predicate

all even [2, 4, 6]
True

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

what does any do?

A

check whether any element satisfies a predicate

any isSpace “ab c”
True

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

what does takeWhile do?

A

selects elements while the predicate holds all elements

takeWhile isAlpha “ab cd”
“ab”

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

what does dropWhile do?

A

teh opposite of takeWhile
dropWhile is Alpha “ab cd”
“ cd”

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

express [fx | x

A

map f( filter p xs)

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

what is ad-hoc polymorphism?

A

overloading

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

What are the 2 types of type declaration?

A

abbreviation type and new type

17
Q

What is an abbreviation type?

A

an existing type with a new name.

18
Q

declare a new type Bool that can be shown and compared

A

data Bool = False | True

deriving (Show, Eq)

19
Q

give an example of a new type where the constructors have parameters

A

data Shape = Circle Float

| Rect Float Float

20
Q

define a new inductive type for natural numbers

A

data nat = Zero | Succ Nat

where
Zero :: Nat
Succ :: Nat -> Nat
Succ = +1

21
Q

define a binary tree type

A

data Tree = Leaf Int

| Node Tree Int Tree

22
Q

write a function that determines whether in Int occurs in a tree

A
occurs :: Int -> Tree -> Bool
occurs m (leaf n) = m == n
occurs m (Node l n r )  = m == n || occurs m l || occurs m r
23
Q

what does flatten do?

A

returns all of the Ints in a tree

24
Q

write a definition of flatten

A

flatten :: tree -> [Int]
flatten (Leaf n) = [n]
flatten (Node l n r) = flatten l ++ [n] ++ flatten r