1 / 17

Writing functions in Haskell

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...

gflack
Télécharger la présentation

Writing functions in Haskell

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing functions in Haskell Part 2 Let, guards, where, and recursion And exam dates (some votes allowed)

  2. 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...

  3. 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

  4. Back to functions in Haskell • Remember discriminant and others? • Remember discriminant and others!

  5. 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

  6. 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...

  7. 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

  8. 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

  9. 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

  10. 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...

  11. 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

  12. 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

  13. 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...

  14. 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...

  15. 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

  16. 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

  17. ackermann m n • Computes the specified value of the Ackermann function • Be very careful with parentheses!

More Related