Haskell, lesson 6
Functions are the heart of Haskell. A function takes inputs and gives back an output, and that's all it does: given the same inputs it always returns the same result. This property, called purity, is what makes Haskell code easy to test and reason about.
The signature Int -> Int reads "takes an Int, returns an Int". The definition names the
parameter n and gives the result after =. There is no return keyword; the expression
on the right is the result.
You call a function by writing its name followed by its arguments, separated by spaces:
square 7 is 49. Use parentheses when an argument is itself an expression:
.
2 exercises to try what you just read.
square (3 + 4)The signature lists each parameter type, then the result type, all joined by arrows. The reason for this arrow-chain style is currying: in Haskell, a two-parameter function is really a function that takes one argument and returns another function waiting for the second. That's what lets you supply only some arguments:
A function can have several equations, one per pattern. Haskell uses the first one that matches. Combine them with guards (from lesson 4) as needed:
where attaches local definitions to a function. They can use the function's parameters,
and they're invisible everywhere else:
That's Haskell's scope rule in a nutshell: a name is visible in the definition where it
was introduced (parameters, let, where), and top-level names are visible in the whole
module. Two functions can each have their own value without clashing.
Recursion replaces loops. Write a base case, then a case that solves a smaller problem:
Integer has no size limit, so factorial 25 gives the exact answer.
Functions that print or read have an IO result type. Keep them small and push the real
work into pure functions:
Write shippingCost :: Int -> Int with guards, using these rules:
The first input line is the number of parcels. Each following line holds one whole-number
weight. For each parcel print <weight> kg costs <cost>, then print Total: <sum>.
Sample input:
Sample output:
The starter already handles input and output; you only write shippingCost.
You have some candies and some stickers and want to fill as many identical party bags as possible, using everything. The number of bags is the greatest common divisor (GCD) of the two amounts.
Write a recursive gcd' :: Int -> Int -> Int using Euclid's rule: the GCD of a and 0 is
a; otherwise the GCD of a and b equals the GCD of b and a `mod` b. (The Prelude
already has a gcd, so the apostrophe gives yours a different name. Don't use the built-in
one; the point is to write it yourself.)
Read the candies and the stickers (two whole numbers, at least 1, on one line separated by a space) and print:
Sample input:
Sample output:
square :: Int -> Int
square n = n * npriceWithTax :: Double -> Double -> Double
priceWithTax price rate = price + price * ratewithVat :: Double -> Double
withVat price = priceWithTax price 0.2ticketPrice :: Int -> Int
ticketPrice age
| age < 12 = 5
| age >= 65 = 6
| otherwise = 10bmi :: Double -> Double -> String
bmi weight height
| value < 18.5 = "under"
| value < 25 = "normal"
| otherwise = "over"
where
value = weight / (height * height)factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)report :: String -> Int -> IO ()
report name score = putStrLn (name ++ ": " ++ show score)3
1
4
231 kg costs 5
4 kg costs 12
23 kg costs 31
Total: 48Bags: <n>
Each bag: <c> candies, <s> stickers12 18Bags: 6
Each bag: 2 candies, 3 stickers