Haskell Practice Prompts Flashcards

1
Q

Use div, /, and mod on two ints, inline

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

Make a character list, return the first element, and the last element

A

use head and tail

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

Using a character list, make two new lists. Use the first half of the list for one, and the second half of the list for the other

A

Use drop and take

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

use a function to determine the largest value in a list.

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

Multiply two lists together

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

Make a list containing 100 a’s, then output the 99th element from that list

A

use repeat or replicate, then use !! 99

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

Make two lists, and combine them into tuples of each index

A

use zip

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

Question: what are the two ways of writing multiline commands in ghci?

A

:{
–your stuff here
:}
or
:set +m

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

What is a type class? Create a function that take an arbitrary list type as input

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

How do you find the type of a function? Find the parameterized types of “zipwith”

A

This can be achieved using :t function

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

What is a class constraint? Name some common class constraints, then implement one in a function.

A

(const a) => [a] -> a
class constraints place constraints on what values can be inputted as arbitrary data types. ex. Eq, Ord, Show, Num, Read, Enum

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

implement where in a function

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

implement let in a function

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

implement multiple variables in a function using let

A

let a = 100; b = 200 in

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

implement case

A

head’ :: [a] ‐> a;
head’ xs = case xs of
[] ‐> error “No head for empty lists!”;
(x:_) -> x

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

Implement case using where instead

A

describeList :: [a] ‐> String
describeList ls = “The list is “ ++ what ls
where
what [] = “empty.”
what [x] = “has one element.”
what xs = “has many elements.”

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

Design a recursive function

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

use fold and lambda to count the number of a’s in a [Char]

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

create a curried function, then use it

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

using curried functions, divide two integers

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

using concatenation, turn an Int list into a [Char] string

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

implement a zipWith’ function using recursion

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

use the map function, to apply a function to every element in a list

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

implement filter’ using recursion

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
use filter to filter out all a's from a string
26
What is the collatz chain?
chain :: Integer ‐> [Integer] chain 1 = [1] chain n |evenn =n:chain(n`div`2) |True =n:chain(3*n+1)
27
use a lambda function with map on a list
28
use a case inside of a lambda function
week 4 p 30
29
use foldr and foldl on the same problem of dividing a list of integers
30
implement elem'
week4 p 38
31
What does the dot operator do?
𝑓·𝑔𝑥 = f (g(x)) just places it inside the parentheses
32
Implement a module and import it into ghci
module Shapes ( Point(..) , Shape(..) , area , nudge , baseCircle , baseRect ) where
33
implement a module and import it into another module
module Shapes ( Point(..) , Shape(..) , area , nudge , baseCircle , baseRect ) where
34
implement any to see if there are any 5's in a list
Week 4 p.73
35
Create a map from a list, then make a query
week 4 ~ p48
36
Create your own data type, then use it in a function
37
Make a type (object), then call the constructor to make one
data Car = Car { company :: String, model :: String, year :: Int } deriving (Show) Call the constructor: Car {company = "Ford", model = "Mustang", year = 1967}
38
Make a data type that handles (Maybe a) as an input
data Maybe a = Nothing | Just a
39
Make a type that contains a generic variable, then construct one, then call the generic variable.
data Car a = Car { company :: String, model :: String, year :: a } deriving (Show)
40
Make a type, then parameterize that type as input for a function
tellCar :: (Show a) => Car a ‐> String tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y
41
Make a vector type, and then use it as inputs for vector dot product calculation
data Vector a = Vector a a a deriving (Show) dotProd :: (Num a) => Vector a ‐> Vector a ‐> a (Vector i j k) `dotProd` (Vector p q r) = (i*p) + (j*q) + (k*r)
42
use the cons operator
5 `Cons` Empty 4 `Cons` (5 `Cons` Empty) 3 `Cons` (4 `Cons` (5 `Cons` Empty))
43
Write an infix function
infixr 5 :‐: data List a = Empty | a :‐: (List a) deriving (Show, Read, Eq, Ord) slide 66 week 5
44
Concatenate two strings
45
Make a tree data structure and insert a value
46
Make a type class
class YesNo a where yesno :: a ‐> Bool instance YesNo Int where yesno 0 = False yesno _ = True
47
Make a lambda function call
48
What is a first class function? Implement one
First class functions pass functions in as arguments
49
Write functions named inc, double, and square that increment, double, and square an argument n, respectively.
50
Write a function that takes a value n. If n is even, the function returns n - 2, and if the number is odd, the function returns 3 × n + 1. To check whether the number is even, you can use either Haskell’s even function or mod (Haskell’s modulo function).
51
Haskell has a function called repeat that takes a value and repeats it infinitely. Using the functions you’ve learned so far, implement your own version of repeat.
52
Write a function subseq that takes three arguments: a start position, an end posi- tion, and a list. The function should return the subsequence between the start and end. For example:
53
Write a function inFirstHalf that returns True if an element is in the first half of a list, and otherwise returns False.
54
The tail function in Haskell returns an error when called on an empty list. Mod- ify myTail so that it does handle the case of an empty list by returning the empty list.
55
Implement your own version of reverse, which reverses a list.
56
Use filter and length to re-create the elem function.
57
Your isPalindrome function from lesson 6 doesn’t handle sentences with spaces or capitals. Use map and filter to make sure the phrase “A man a plan a canal Panama” is recognized as a palindrome.
58
In mathematics, the harmonic series is the sum of 1/1 + 1/2 + 1/3 + 1/4 .... Write a function harmonic that takes an argument n and calculates the sum of the series to n. Make sure to use lazy evaluation.
59
What is the type signature for filter? How is it different from map?
60
In Haskell, both tail and head have an error when called on an empty list. You can write a version of tail that won’t fail but instead return an empty list when called on an empty list. Can you write a version of head that returns an empty list when called on an empty list? To answer this, start by writing out the type signatures of both head and tail.
61
What is a type? What is a type class? How do they differ?
A type is like an object. A type class is the constraint placed on it, like Enum, Ord, or Show
61
What is a type? What is a type class? How do they differ?
A type is like an object. A type class is the constraint placed on it, like Enum, Ord, or Show
62
Define a letter type, an interest rate type, and an isFun type, Char, Double, and Bool respectively
letter :: Char letter = 'a' interestRate :: Double interestRate = 0.375 isFun :: Bool isFun = True
63
Define a function type, then a function definition
double :: Int -> Int double n = n*2
64
What is a type synonym? Implement one
A type synonym is a "rebranded" type. Like if I say type car :: String then car = "mustang" now I can parameterize the car type in functions myCar :: car -> String myCar = car
65
How do you make a new type? Make one
data Sex = Male | Female data Covid = Pos | Neg -- Here we see that sexAndStatus calls its constructor using both Sex and Covid data sexAndStatus = sexAndStatus Sex Covid
66
use record syntax to make a Patient object, with sex, age, and height, create a Patient object, then use these types in a function
67
Implement a function that returns a maybe variable, then force it to return nothing
68
Make a Haskell script, compile it from terminal, then run It in terminal
69
Make a Haskell script, compile it from terminal, then pipe it to a text file
70
What is nub?
Removes duplicate elements from a list
71
what is fmap?
Applies a function to a functor
72
What is a functor?
things that can be mapped over, like maybes, Lists, Maps, and trees. These are all functors. Functors use data types that are wrapped in a type class, like a Maybe or Either. A functor has to implement a fmap function, which tells it how it applies functions... This is because you can't apply functions to a Just variable... It needs to look inside of the Just.
73
Write and use a Haskell script that takes command arguments
74
use read but dictate the returned type
read "5" :: Int
75
put a where clause after a guard. for instance, area of a rectangle
| area < 100 where area = x * y other area = y * x
76
Implement let.
cylinder :: (RealFloat a) => a -> a -> a cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r ^2 in sideArea + 2 * topArea
77
What is <$> equivalent to?
fmap
78
What is <*> called?
Applicative functor
79
Give an example of a list comprehension
[x*2 | x <- [1..10]]