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