4. Defining functions Flashcards

1
Q

Function to split list at a specific index

A

splitAt :: Int -> [a] -> ([a], [a])
splitAt n xs = (take n xs, drop n xs)

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

What is the syntax of a conditional expression

A

abs :: Int -> Int
abs n = if n >= 0 then n else -n

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

What is the syntax of a guarded equation?

A

signum n | n < 0 = -1
| n == 0 = 0
| otherwise = 1

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

Function that uses a lambda expression for getting odd numbers from 0 to n

A

odds :: Int -> [Int]
odds n = map (\x -> x * 2 + 1) [0..n-1]

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