Haskell Flashcards
Dot operator type
(b -> c) -> (a -> b) -> a -> c
exOr x y =
(x || y) && not(x && y)
Offset between a lowercase character and its uppercase counterpart
32
ord ‘a’ = 97; ord ‘A’ = 65
Read and show
Read: convert from a string to a value
Show: Convert from a value to a string
C enumeration vs Java enumeration
C: Integers and enum types can be mixed freely
Java: Defines a new type
Switch case in Java, C, C++
Default is to fall through. Prevented by ‘break’
Switch case in Prolog, C#
Cannot fall through. permitted by ‘next’
Evaluate the inline conditional expression: (x % 2 == 0 ? “even” : “odd)
if (x % 2 == 0) {“even”}
else {“odd”}
What is an Ivalue in C++
An Ivalue is an expression that yields an object reference
T-square evolution algorithm
- Start with a square
- At each convex corner, place another square, centered at the corner, with half the side length of the previous square
- Repeat step 2
T-square evolution recursive parameters
Width decreased by 1/2, Step is decreased by 1, Fill the square
Functions used on tuples of size 2 (pairs)
‘fst’ to retrieve first element
‘snd’ to retrieve second element
Lists in Haskell vs. Arrays in C/C++/Java
Memory in arrays is contiguous;
Any element can be accessed in constant time
head :: [a] -> a
Returns the first element in a list
tail :: [a] -> [a]
Returns a list with all elements in a list except for the first element
take :: Int -> [a] -> [a]
Returns a list with the first n elements in a list
drop :: Int -> [a] -> [a]
Returns the list after removing its first n elements
length :: [a] -> Int
Returns number of elements in a list
: :: a -> [a] -> [a]
Add a single element to the front of a list
++ :: [a] -> [a] -> [a]
Join two lists
!! :: [a] -> Int -> a
Returns the nth element of a list starting from 0
reverse :: [a] -> [a]
Reverse the order of elements
zip :: [a] -> [b] -> [(a, b)]
Take a pair of lists into a list of pairs
unzip :: [(a, b)] -> ([a], [a])
Take a list of pair into a pair of lists
sum :: [Int]/[Float] -> Int/Float
Sum of a numeric list
(x:xs)
Splits a list into its head and tail;
x is the head, xs is the tail
List Comprehension General form
[Expression involving var | var <- old list, conditions, …, conditions]
What is ‘var <- old list’ called
Generator. iterates over the old list, multiple generators can be used
Higher order function
A function that takes one or more functions as a parameter and/or returns a function as its result
Map general form
map :: (a -> b) -> [a] -> [b];
function (a -> b) is applied to every element in [a] and the result is the new list [b]
General form of lambda function
<arg1>…<arg> -> <expression></expression></arg>
Filter function
First argument is a predicate (returns a boolean), second argument is a list