170 likes | 182 Views
Learn how to write functions in Haskell using let, guards, where, and recursion. Understand discriminants, find speeding costs, and solve quadratic equations with ease. ###
E N D
Writing functions in Haskell Part 2 Let, guards, where, and recursion And exam dates (some votes allowed)
But first... • Your next homework assignment is now available and is due a week from today • You will need to work individually on writing functions in Haskell • Let's take a look... • And let's talk about tuples...
Upcoming dates • Exam 2: Tuesday, April 17 • Exam 3: either: • Tuesday, May 1 • Thursday, May 3 • Time for discussion • Voting is deferred until this Thursday
Back to functions in Haskell • Remember discriminant and others? • Remember discriminant and others!
numRealSolutions a b c • Returns the number of solutions to the quadratic equation below • If the discriminant is negative, zero real solution • If the discriminant is zero, one real solution • If the discriminant is positive, two real solutions • numRealSolutions 1 2 3 0 • numRealSolutions 3 0 0 1 • numRealSolutions (-1) (-2) (3) 2
let bindings • 'let' allows us to create variables that only exist for a single expression • The syntax is: • let variable assignmentsin expression • Let's see some examples...
cylinderSurfaceArea radius height • Returns the surface area of a cylinder with the given radius and height • Two parts to consider: surface area of caps and surface area of the body of the cylinder • Using let can make this code a lot easier to read • cylinderSurfaceArea 2 7 113.09733552923255 • cylinderSurfaceArea 10 3 816.8140899333462
speedingCostdist time • Calculates and returns the fine that you would have for speeding. Distance is in miles and time is in hours. The fines are as follows: • less than 55 mph: $0 fine • 55 – 65: $100 fine • 65 – 85: $700 fine • 85+: $5000 fine • Examples: • speedingCost 200 3 700 • speedingCost 100 3 0 • speedingCost 5 0 5000
numRealSolutions' a b c • Returns the number of solutions to the quadratic equation below (using let to make our lives easier) • If the discriminant is negative, zero real solution • If the discriminant is zero, one real solution • If the discriminant is positive, two real solutions • numRealSolutions 1 2 3 0 • numRealSolutions 3 0 0 1 • numRealSolutions (-1) (-2) (3) 2
Guards! • Guards allow us to have different expressions used as the answer depending on whether a condition is true • You can view them as being akin to a series of if, else if, else if, else statements • Let's look at some examples...
speedingCost' dist time • Calculates and returns the fine that you would have for speeding. Distance is in miles and time is in hours. Uses guards instead of if. The fines are as follows: • less than 55 mph: $0 fine • 55 – 65: $100 fine • 65 – 85: $700 fine • 85+: $5000 fine • Examples: • speedingCost 200 3 700 • speedingCost 100 3 0 • speedingCost 5 0 5000
numRealSolutions'' a b c • Returns the number of solutions to the quadratic equation below (using guards instead of if) • If the discriminant is negative, zero real solution • If the discriminant is zero, one real solution • If the discriminant is positive, two real solutions • numRealSolutions 1 2 3 0 • numRealSolutions 3 0 0 1 • numRealSolutions (-1) (-2) (3) 2
Avoiding repetition with guards • We can't easily use let with guards • Instead, we can use 'where' to specify variables to be defined for an entire function • We place this at the end, indented relative to the function definition line • Let's see how this can simplify the previous two functions...
Recursion • The way that we loop or iterate in Haskell is via recursion • For recursion to work, we need: • A base case • A recursive case that expresses the answer in terms of an answer to a simpler problem • In Haskell, we can express this using if expressions or guards • Let's see some examples...
factorial n • Returns n factorial where this is equal to the product of all the integers between n and 1, inclusive • For any values 1 or smaller, answer 1 • If you hit an infinite loop, Control-C should cancel it • You might need to use parentheses to get things to parse how you want • Examples: • factorial 1 1 • factorial 10 3628800
fib n • Computes the nth Fibonacci number • If you hit an infinite loop, Control-C should cancel it • You might need to use parentheses to get things to parse how you want
ackermann m n • Computes the specified value of the Ackermann function • Be very careful with parentheses!