cs302 paradigms of programming functional programming
play

CS302: Paradigms of Programming Functional Programming with Haskell - PowerPoint PPT Presentation

CS302: Paradigms of Programming Functional Programming with Haskell (Cont.) Manas Thakur Spring 2020 The best way to begin (and even finish!) 2 Types types types What is a type? What is it useful for? What is type checking? Di


  1. CS302: Paradigms of Programming Functional Programming with Haskell (Cont.) Manas Thakur Spring 2020

  2. The best way to begin (and even finish!) 2

  3. Types types types • What is a type? • What is it useful for? • What is type checking? • Di ff erence between a strongly and a weakly typed language? • Why shouldn’t we always use the most general type? Duck typing! 
 • Static vs dynamic typing? If it walks like a duck 
 and it quacks like a duck, 
 • Untyped languages? then it must be a duck. • Haskell tries to o ff er the best of all worlds: Type inference! 3

  4. Abstract Data Types • Type with operations • What’s abstract about it? • Hides the representation details. • Recall our Point example from Lab2 . • Definition of Bool in Haskell: data Bool = False | True Data declaration Data constructors 4

  5. Examples of ADTs Named constructors! data Shape = Circle Float Float Float | Rectangle Float Float Float Float • What are constructors in OO languages? • Check the types of Circle and Rectangle . • Now you can use these types in functions: area :: Shape -> Float Notice 
 area (Circle _ _ r) = pi * r * r pattern matching! area (Rectangle ... • Why won’t area :: Circle -> Float work? • Same reason why foo :: True -> Int won’t work! 5

  6. Type Parameters data Maybe a = Nothing | Just a Type parameter • Maybe is a very useful type for handling failures. head :: [a] -> a safeHead :: [a] -> Maybe a head [] = error “Empty list” safeHead [] = Nothing head (x:_) = x safeHead (x:_) = Just x Can’t return this! safediv :: Int -> Int -> Maybe Int safediv _ 0 = Nothing safediv m n = Just (m `div` n) 6

  7. Type Classes • Check the type of Haskell’s built-in sort function: > import Data.List > :t sort > sort :: Ord a => [a] -> [a] • It sorts lists containing elements of type a such that a belongs to the type class Ord . • What’s the problem with the following types for + : (+) :: Int -> Int -> Int Too specific (+) :: Float -> Float -> Float (+) :: a -> a -> a Too general • We would like to define (+) for only number-like types. 7

  8. Type Classes (Cont.) a is a type variable • Check the type of + now: > :t (+) > + :: Num a => a -> a -> a • Here is the declaration of the type class Num from Haskell: class (Eq a, Show a) => Num a where (+),(-),(*) :: a -> a -> a negate :: a -> a abs, signum :: a -> a fromInteger :: Integer -> a • Class Num is a subclass of both Eq and Show . • +, -, *, negate, … are predefined functions in Num . 8

  9. Recursive Types • What does this type describe? data Nat = Zero | Succ Nat • Does this remind of something from Lab1 ? • Church numerals! nat2int :: Nat -> Int nat2int Zero = 0 nat2int (Succ n) = 1 + nat2int n add :: Nat -> Nat -> Nat add m n = int2nat (nat2int m + nat2int n) CQ 9

  10. The best way to finish (and even begin!) 10

  11. On roll for the last week 1. Type Inference 3. Monads 2. Proving Program 
 Properties T-Week 11

Recommend


More recommend