Week 35 Flashcards
Simple functions
How is a function defined in Haskell?
name of the function + variable + the function in it self
sq x = x * x
Simple functions
What does the program do when running bar(bar3)?
First it runs the bar function with the varaible 3. Then it runs the bar function again but with bar(3) as a varaible.
Bool
How can you use the if and else statments in Haskell?
doubleSmallNumber x = if x > 100
then x
else x * 2
The “else” part is mandatory, every expresion needs a value in Haskell
Bool
Solve:
The price of prawns: 270/Kg. Define a function price to calculate the cost of a given weight of prawns. Suppose price gets cheaper if we buy more: unit price/Kg up to 50 kg, and a discount of 20% per Kg for everything above 50kg, plus an additional 10% above 100 kg
price :: Double -> Double
priceKg x =
| 50 < x > 100 = x * 270 * 0.8
| x > 100 = x * 270 * 0.7
| otherwise = x * 270
Pattern Matching
What is pattern matching?
Specifying patterns to which some data should conform and then checking to see if it does and deconstructing the data according to those patterns
Pattern Matching
There is 4 major types of pattern matching used in Haskell, what are they?
fmatchesanything at all, and binds thefvariable to whatever is matched
(x:xs)imatches anon-empty listwhich is formed by something (which gets bound to thexvariable) onto something else (which gets bound toxs).
[]is a pattern that matchesthe empty list. It doesn’t bind any variables.
**_ **is the pattern which matches anything without binding
Logic
What logical operators are used in Haskell and what do they mean
ghci>True && True (and)
True
ghci> False || True (or)
True
ghci> not False
True
Logic
What operators are used to test form equalitys in Haskell?
ghci> 5 == 5 (is equall to)
True
ghci> 5 /= 5 (is greater than )
False
ghci> 5 =/ 5 (is less than)
False
Values
What functions are used to get the ma or min of a number of arguments?
ghci> min 9 10
9
ghci> max 100 101
101
Values
What function are used to get the next variable for inputs that can be ranked?
ghci> succ 9
10
ghci> succ a
b
Values
What function are used to calculate modules?
ghci> 4 ´mod´ 2
2
Values
What function is used to divide 2 arguments?
ghci> 6 ´div´2
3
Values
What function converts a type to a integral
fromIntegral :: (Num b, Integral a) => a -> b
Recursion
What is recursion in haskell?
A way of defining functions in which the function is applied inside its own definition.
Recursion
Write a program which calulates the power of a exponent?
power n k | k < 0 = error “Negative exponent.”
power n 0 = 1
power n k = power n (k-1) * n
power is the name of the function
n and k is the functions’ arguments
th
Write a program for factorial numbers
fac 0 = 1
fac n = n * fac (n-1)
Recursion
Write a program for the fibbonacci sequence
fib
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
Recursion
Write a program which reverses a list
reverse’ :: [a] -> [a]
reverse’ [] = []
reverse’ (x:xs) = reverse’ xs ++ [x]
Lists
What is a list in Haskell?
Lists stores several elements of the SAME type
Lists
Write 3 lists; the first is an empty list, the second is a empty list within another list
[] (empty list)
[ [] ] (a list containg an empty list)
List
What are strings?
Strings are lists of characters
“hello” = [’h’,’e’,’l’,’l’,’o’]
List functions works on strings aswell
List - functions