Haskell Practice Prompts Flashcards
Use div, /, and mod on two ints, inline
Make a character list, return the first element, and the last element
use head and tail
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
Use drop and take
use a function to determine the largest value in a list.
Multiply two lists together
Make a list containing 100 a’s, then output the 99th element from that list
use repeat or replicate, then use !! 99
Make two lists, and combine them into tuples of each index
use zip
Question: what are the two ways of writing multiline commands in ghci?
:{
–your stuff here
:}
or
:set +m
What is a type class? Create a function that take an arbitrary list type as input
How do you find the type of a function? Find the parameterized types of “zipwith”
This can be achieved using :t function
What is a class constraint? Name some common class constraints, then implement one in a function.
(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
implement where in a function
implement let in a function
implement multiple variables in a function using let
let a = 100; b = 200 in
implement case
head’ :: [a] ‐> a;
head’ xs = case xs of
[] ‐> error “No head for empty lists!”;
(x:_) -> x
Implement case using where instead
describeList :: [a] ‐> String
describeList ls = “The list is “ ++ what ls
where
what [] = “empty.”
what [x] = “has one element.”
what xs = “has many elements.”
Design a recursive function
use fold and lambda to count the number of a’s in a [Char]
create a curried function, then use it
using curried functions, divide two integers
using concatenation, turn an Int list into a [Char] string
implement a zipWith’ function using recursion
use the map function, to apply a function to every element in a list
implement filter’ using recursion
use filter to filter out all a’s from a string
What is the collatz chain?
chain :: Integer ‐> [Integer]
chain 1 = [1]
chain n
|evenn =n:chain(ndiv
2) |True =n:chain(3*n+1)
use a lambda function with map on a list
use a case inside of a lambda function
week 4 p 30
use foldr and foldl on the same problem of dividing a list of integers
implement elem’
week4 p 38
What does the dot operator do?
𝑓·𝑔𝑥 = f (g(x))
just places it inside the parentheses
Implement a module and import it into ghci
module Shapes
( Point(..)
, Shape(..)
, area
, nudge
, baseCircle
, baseRect
) where
implement a module and import it into another module
module Shapes
( Point(..)
, Shape(..)
, area
, nudge
, baseCircle
, baseRect
) where
implement any to see if there are any 5’s in a list
Week 4 p.73
Create a map from a list, then make a query
week 4 ~ p48
Create your own data type, then use it in a function
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}
Make a data type that handles (Maybe a) as an input
data Maybe a = Nothing | Just a
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)
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
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) = (ip) + (jq) + (k*r)
use the cons operator
5 Cons
Empty
4 Cons
(5 Cons
Empty)
3 Cons
(4 Cons
(5 Cons
Empty))
Write an infix function
infixr 5 :‐:
data List a = Empty | a :‐: (List a)
deriving (Show, Read, Eq, Ord)
slide 66 week 5
Concatenate two strings
Make a tree data structure and insert a value
Make a type class
class YesNo a where
yesno :: a ‐> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
Make a lambda function call
What is a first class function? Implement one
First class functions pass functions in as arguments
Write functions named inc, double, and square that increment, double, and square an argument n, respectively.
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).
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.
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:
Write a function inFirstHalf that returns True if an element is in the first half of a list, and otherwise returns False.
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.
Implement your own version of reverse, which reverses a list.
Use filter and length to re-create the elem function.
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.
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.
What is the type signature for filter? How is it different from map?
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.
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
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
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
Define a function type, then a function definition
double :: Int -> Int
double n = n*2
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
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
use record syntax to make a Patient object, with sex, age, and height, create a Patient object, then use these types in a function
Implement a function that returns a maybe variable, then force it to return nothing
Make a Haskell script, compile it from terminal, then run It in terminal
Make a Haskell script, compile it from terminal, then pipe it to a text file
What is nub?
Removes duplicate elements from a list
what is fmap?
Applies a function to a functor
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.
Write and use a Haskell script that takes command arguments
use read but dictate the returned type
read “5” :: Int
put a where clause after a guard. for instance, area of a rectangle
area < 100
where area = x * y
other area = y * x
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
What is <$> equivalent to?
fmap
What is <*> called?
Applicative functor
Give an example of a list comprehension
[x*2 | x <- [1..10]]