190 likes | 216 Views
Computer Science 312. Haskell Data Types and Functions. Basic Haskell Data Types. Check ‘em out with :type. Prelude> :type 'a' 'a' :: Char Prelude> :type "Hi there" "Hi there" :: [Char] Prelude> :type 45 45 :: Num a => a Prelude> :type odd odd :: Integral a => a -> Bool :type 3.14
E N D
Computer Science 312 Haskell Data Types and Functions
Check ‘em out with :type Prelude> :type 'a' 'a' :: Char Prelude> :type "Hi there" "Hi there" :: [Char] Prelude> :type 45 45 :: Num a => a Prelude> :type odd odd :: Integral a => a -> Bool :type 3.14 3.14 :: Fractional a => a Prelude> :type sqrt sqrt :: Floating a => a -> a Num, Integral, Fractional, and Floating are type classes
What Is a Type Class? • A type class allows you to group together several types (also called type instances) into a single category • For example, the types Int, Integer, Float, and Double are instances of the type class Num • Type classes allow you to overload operators for different types of data • Type classes are organized in a type hierarchy
Variables and Assignment Prelude> pi 3.141592653589793 Prelude> area = pi * 6 ^ 2 -- New in GHCI 8.2.2 Prelude> area 113.09733552923255 Variables are not capitalized. Single assignment only in a module, but not in the GHCI.
To let or Not to let Prelude> pi 3.141592653589793 Prelude> let area = pi * 6 ^ 2 Prelude> area 113.09733552923255 Same semantics as simple assignment, but allowed in the GHCI only.
Defining Simple Functions Prelude> square n = n * n Prelude> square 3 9 Prelude> cube n = n * (square n) Prelude> cube 3 27 Prelude> between n low high = low <= n && n <= high Prelude> between 5 1 10 True Look like simple equations
Package in a Module {- File: NewMath.hs Author: Ken Lambert Purpose: provides some simple math functions -} module NewMath where -- Function to square an integer square n = n * n -- Function to cube an integer cube n = n * square n -- Function to test for inclusion in a range between n low high = low <= n && n <= high
Type Inference -- Function to square an integer square n = n * n -- Function to cube an integer cube n = n * square n -- Function to test for inclusion in a range between :: Integer -> Integer -> Integer -> Bool between n low high = low <= n && n <= high Haskell infers the types of the parameters and the function’s return value if the type signature is not included Including type signatures is standard practice, however
Type Signatures -- Function to test for inclusion in a range between :: Integer -> Integer -> Integer -> Bool between n low high = low <= n && n <= highTrue Specify the types of parameters and the return type of the function, at compile time
Include Type Signatures {- File: NewMath.hs Author: Ken Lambert Purpose: provides some simple math functions -} module NewMath where -- Function to square an integer square :: Integer -> Integer square n = n * n -- Function to cube an integer cube :: Integer -> Integer cube n = n * square n -- Function to test for inclusion in a range between :: Integer -> Integer -> Integer -> Bool between n low high = low <= n && n <= high
Generalize the Types {- File: NewMath.hs Author: Ken Lambert Purpose: provides some simple math functions -} module NewMath where -- Function to square an integer square :: Num a => a -> a square n = n * n -- Function to cube an integer cube :: Num a => a -> a cube n = n * square n -- Function to test for inclusion in a range between :: Ord a => a -> a -> a -> Bool between n low high = low <= n && n <= high
Simple Recursive Functions n! = 1, when n = 1 n! = n * (n – 1)!, when n > 1 factorial :: Integer -> Integer factorial 1 = 1 factorial n = n * factorial (n – 1) A recursive function has at least two equations, or clauses One equation states the base case The other equation states the recursive step
Exercise 1! Define the function expo in the NewMath module. This function expects a non-negative integer base and exponent as arguments and returns the base raised to the exponent. The function returns 1, when the exponent is 0, or the base multiplied by a recursive call on the base and the exponent minus 1, otherwise. Load the module into the GHCi and test your function.
Exercise 2! A standard science experiment is to drop a ball and see how high it bounces. Once the "bounciness" of the ball has been determined, the ratio gives a bounciness index. For example, if a ball dropped from a height of 10 feet bounces 6 feet high, the index is 0.6 and the total distance traveled by the ball is 16 feet after one bounce. If the ball were to continue bouncing, the distance after two bounces would be 10 ft + 6 ft + 6 ft + 3.6 ft = 25.6 ft. Note that distance traveled for each successive bounce is the distance to the floor plus 0.6 of that distance as the ball comes back up. Define a function bouncy that expects as arguments the initial height of the ball, its bounciness index, and the number of times the ball is allowed to continue bouncing. The function returns the total distance traveled by the ball.
Quitting the Shell Prelude> :q When a process hangs, ctrl-c can exit the shell.
For next time Making choices Dealing with errors Local contexts with where