Computer Science 312 Haskell Data Types and Functions

  • Slides: 19
Download presentation
Computer Science 312 Haskell Data Types and Functions

Computer Science 312 Haskell Data Types and Functions

Basic Haskell Data Types

Basic Haskell Data Types

Check ‘em out with : type Prelude> : type 'a' : : Char Prelude>

Check ‘em out with : type Prelude> : type 'a' : : Char Prelude> : type "Hi there" : : [Char] Prelude> : type 45 45 : : Num a => a Prelude> : type odd : : Integral a => a -> Bool : type 3. 14 : : Fractional a => a Prelude> : type 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

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

The Numeric Type Class Hierarchy

The Numeric Type Class Hierarchy

Variables and Assignment Prelude> pi 3. 141592653589793 Prelude> area = pi * 6 ^

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 =

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

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: New. Math. hs Author: Ken Lambert Purpose: provides some

Package in a Module {File: New. Math. hs Author: Ken Lambert Purpose: provides some simple math functions -} module New. Math 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

Load and Test the Module

Load and Test the Module

Type Inference -- Function to square an integer square n = n * n

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

Type Signatures -- Function to test for inclusion in a range between : : Integer -> Bool between n low high = low <= n && n <= high. True Specify the types of parameters and the return type of the function, at compile time

Include Type Signatures {File: New. Math. hs Author: Ken Lambert Purpose: provides some simple

Include Type Signatures {File: New. Math. hs Author: Ken Lambert Purpose: provides some simple math functions -} module New. Math 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 -> Bool between n low high = low <= n && n <= high

Generalize the Types {File: New. Math. hs Author: Ken Lambert Purpose: provides some simple

Generalize the Types {File: New. Math. hs Author: Ken Lambert Purpose: provides some simple math functions -} module New. Math 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 -> Bool between n low high = low <= n && n <= high

Simple Recursive Functions n! = 1, when n = 1 n! = n *

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 New. Math module. This function expects

Exercise 1! Define the function expo in the New. Math 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

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

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

For next time Making choices Dealing with errors Local contexts with where