1 / 13

Writing functions in Haskell

Learn how to define variables, write functions, handle if expressions, and solve quadratic equations in Haskell. Get started with examples and explanations in this helpful guide. Textbook: "Learn you a Haskell for Great Good."

newby
Download Presentation

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 1

  2. But first... • Any questions on your interpreter assignment? • Any issues getting Haskell working on your computer? • Any questions about the feedback on your project proposal?

  3. Textbook for learning Haskell • "Learn you a Haskell for Great Good" • http://learnyouahaskell.com • You are not required to do any reading from this since we'll be covering everything in class • It is still a fantastic (free!) reference for using Haskell

  4. Defining variables • In Haskell, we define variables using an assignment statement • Variables can only be assigned to once, and get their type based on what they are initialized to • Variable names start with a lowercase letter and consist of letters, numbers, and some symbols. • Let's look at examples...

  5. Haskell and source code files • Code you write in Haskell should be in *.hs files • These files can be compiled or loaded into GHCi • Within the REPL, you can test what you've written • In the REPL, you can load a file using :load (or :l (lowercase L)) • The REPL isn't purely functional...

  6. Creating functions • The syntax for creating functions is very similar to that of creating variables • Parameters are placed after the function name and before the equals sign • The right hand side is whatever you want the result of the function to be • Let's see some examples...

  7. volume l w h • Write a function volume that takes three parameters and returns the volume of a box with those dimensions • volume 3 4 5 60 • volume 10 1 2.3 23.0

  8. area r • Write a function area that takes a single parameter and returns the area of the circle with radius r • area 1 3.141592653589793 • area 2 12.566370614359172 • area 3 28.274333882308138

  9. discriminant a b c • Returns the discriminant of the quadratic equation below • discriminant 1 2 3 -8 • discriminant 3 0 0 0 • discriminant (-1) (-2) (3) 16

  10. hasRealSolution a b c • Returns true if the quadratic equation below has one or more real solutions • If the discriminant is negative, zero real solution • If the discriminant is zero, one real solution • If the discriminant is positive, two real solutions • hasRealSolution 1 2 3 False • hasRealSolution 3 0 0 True • hasRealSolution (-1) (-2) (3) True

  11. if expressions • Haskell allows us to have if expressions where based on the truth or falsehood of an expression, one of two expressions is returned if condition then result1 else result2

  12. closestToZero a b • Takes in two numbers and returns the number that is closer to zero on the number line • closestToZero 5 20 5 • closestToZero (-10) 5 5 • closestToZero (-1) (-2) -1

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

More Related