1 / 19

Computer Science 312

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

Download Presentation

Computer Science 312

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. Computer Science 312 Haskell Data Types and Functions

  2. Basic Haskell Data Types

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

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

  5. The Numeric Type Class Hierarchy

  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.

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

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

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

  10. Load and Test the Module

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

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

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

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

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

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

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

  18. Quitting the Shell Prelude> :q When a process hangs, ctrl-c can exit the shell.

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

More Related