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
Q

use filter to filter out all a’s from a string

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

What is the collatz chain?

A

chain :: Integer ‐> [Integer]
chain 1 = [1]
chain n
|evenn =n:chain(ndiv2) |True =n:chain(3*n+1)

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

use a lambda function with map on a list

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

use a case inside of a lambda function

A

week 4 p 30

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

use foldr and foldl on the same problem of dividing a list of integers

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

implement elem’

A

week4 p 38

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

What does the dot operator do?

A

𝑓·𝑔𝑥 = f (g(x))
just places it inside the parentheses

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

Implement a module and import it into ghci

A

module Shapes
( Point(..)
, Shape(..)
, area
, nudge
, baseCircle
, baseRect
) where

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

implement a module and import it into another module

A

module Shapes
( Point(..)
, Shape(..)
, area
, nudge
, baseCircle
, baseRect
) where

34
Q

implement any to see if there are any 5’s in a list

A

Week 4 p.73

35
Q

Create a map from a list, then make a query

A

week 4 ~ p48

36
Q

Create your own data type, then use it in a function

A
37
Q

Make a type (object), then call the constructor to make one

A

data Car = Car {
company :: String,
model :: String,
year :: Int
} deriving (Show)
Call the constructor:
Car {company = “Ford”, model = “Mustang”, year = 1967}

38
Q

Make a data type that handles (Maybe a) as an input

A

data Maybe a = Nothing | Just a

39
Q

Make a type that contains a generic variable, then construct one, then call the generic variable.

A

data Car a = Car {
company :: String,
model :: String,
year :: a
} deriving (Show)

40
Q

Make a type, then parameterize that type as input for a function

A

tellCar :: (Show a) => Car a ‐> String
tellCar (Car {company = c, model = m, year = y}) =
“This “ ++ c ++ “ “ ++ m ++ “ was made in “ ++ show y

41
Q

Make a vector type, and then use it as inputs for vector dot product calculation

A

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) = (ip) + (jq) + (k*r)

42
Q

use the cons operator

A

5 Cons Empty
4 Cons (5 Cons Empty)
3 Cons (4 Cons (5 Cons Empty))

43
Q

Write an infix function

A

infixr 5 :‐:
data List a = Empty | a :‐: (List a)
deriving (Show, Read, Eq, Ord)

slide 66 week 5

44
Q

Concatenate two strings

A
45
Q

Make a tree data structure and insert a value

A
46
Q

Make a type class

A

class YesNo a where
yesno :: a ‐> Bool

instance YesNo Int where
yesno 0 = False
yesno _ = True

47
Q

Make a lambda function call

A
48
Q

What is a first class function? Implement one

A

First class functions pass functions in as arguments

49
Q

Write functions named inc, double, and square that increment, double, and square an argument n, respectively.

A
50
Q

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).

A
51
Q

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.

A
52
Q

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:

A
53
Q

Write a function inFirstHalf that returns True if an element is in the first half of a list, and otherwise returns False.

A
54
Q

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.

A
55
Q

Implement your own version of reverse, which reverses a list.

A
56
Q

Use filter and length to re-create the elem function.

A
57
Q

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.

A
58
Q

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.

A
59
Q

What is the type signature for filter? How is it different from map?

A
60
Q

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.

A
61
Q

What is a type? What is a type class? How do they differ?

A

A type is like an object. A type class is the constraint placed on it, like Enum, Ord, or Show

61
Q

What is a type? What is a type class? How do they differ?

A

A type is like an object. A type class is the constraint placed on it, like Enum, Ord, or Show

62
Q

Define a letter type, an interest rate type, and an isFun type, Char, Double, and Bool respectively

A

letter :: Char
letter = ‘a’
interestRate :: Double
interestRate = 0.375
isFun :: Bool
isFun = True

63
Q

Define a function type, then a function definition

A

double :: Int -> Int
double n = n*2

64
Q

What is a type synonym? Implement one

A

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
Q

How do you make a new type? Make one

A

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
Q

use record syntax to make a Patient object, with sex, age, and height, create a Patient object, then use these types in a function

A
67
Q

Implement a function that returns a maybe variable, then force it to return nothing

A
68
Q

Make a Haskell script, compile it from terminal, then run It in terminal

A
69
Q

Make a Haskell script, compile it from terminal, then pipe it to a text file

A
70
Q

What is nub?

A

Removes duplicate elements from a list

71
Q

what is fmap?

A

Applies a function to a functor

72
Q

What is a functor?

A

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
Q

Write and use a Haskell script that takes command arguments

A
74
Q

use read but dictate the returned type

A

read “5” :: Int

75
Q

put a where clause after a guard. for instance, area of a rectangle

A

area < 100
where area = x * y
other area = y * x

76
Q

Implement let.

A

cylinder :: (RealFloat a) => a -> a -> a
cylinder r h =
let sideArea = 2 * pi * r * h
topArea = pi * r ^2
in sideArea + 2 * topArea

77
Q

What is <$> equivalent to?

A

fmap

78
Q

What is <*> called?

A

Applicative functor

79
Q

Give an example of a list comprehension

A

[x*2 | x <- [1..10]]