130 likes | 134 Views
Writing functions in Haskell. Part 1. 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?. Textbook for learning Haskell. "Learn you a Haskell for Great Good"
E N D
Writing functions in Haskell Part 1
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?
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
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...
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...
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...
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
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
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
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
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
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
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