Haskell and Function Types - Organization of Programming Languages - Lecture Notes, Study notes of Programming Languages

This course is an introduction to concepts in programming languages. The course covers a range of programming paradigms including procedural, functional, and logic-based languages. This lecture includes: Haskell and Function Types, Basic Haskell Types, Boolean Values, Instances Are Types, Whole Number Types, Class Constraints, Function Types

Typology: Study notes

2012/2013

Uploaded on 09/28/2013

noob
noob 🇮🇳

4.4

(25)

105 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture Notes CPSC 326 (Spring 2013)
Today’s Topics ...
Haskell and Function types
Reminder ...
Exam 1 next Thursday (Feb. 7th)
S. Bowers 1 of 10
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Haskell and Function Types - Organization of Programming Languages - Lecture Notes and more Study notes Programming Languages in PDF only on Docsity!

Today’s Topics ...

  • Haskell and Function types

Reminder ...

  • Exam 1 next Thursday (Feb. 7th)

Basic Haskell Types (Revisited)

Char

  • Represents (Unicode) characters (e.g., ‘a’)

Bool

  • Represents Boolean values: True or False

Int

  • Signed, fixed-width integer values
  • Size depends on system (today 32 or 64 bits wide)
  • Other smaller numeric types available as well Integer
  • A signed integer of unbounded size

Double

  • 64 bit floating point numbers (native system representation)
  • Also a Float type, but not used often (smaller, but slower)

Haskell Type Classes

As we’ve seen, types are more complicated for numbers ...

Prelude> :type 49 49 :: (Num t) => t

  • 49 has type t such that t is a member of typeclass Num
  • In other words, 49 can be any type that is a member of the Num typeclass

Num ... number types (must be in Eq and Show)

  • Functions: +, *, - , negate, etc.

Integral ...whole number types (Int and Integer)

  • Int is machine specific, Integer is unbounded
  • Must be of type Real and Enum
  • Functions: rem, div, mod, etc.

Bounded, Floating, Fractional, Real, RealFrac

  • To find out about these type :info Fractional, etc., in ghci
  • See also the Prelude doc

Class constraints

For this type ...

Prelude> :type 49 49 :: (Num t) => t

  • Everything before the => is called a class consraint
  • Only constrains type t to be a member of the Num typeclass

Function types

Functions have types (either given or inferred)

Prelude> not True False

Prelude> :type not not :: Bool -> Bool

  • The -> is read as “to” or “returns”

“not has the type Bool to Bool”

“not takes a Bool and returns a Bool”

Another example

Prelude> succ 6 7

Prelude> :type succ succ :: (Enum a) => a -> a

  • Here we have a class constraint

“for all Enum types a, succ has the type a to a”

Functions with multiple arguments

Example

Prelude> take 4 [1, 3 .. 21] [1, 3, 5, 7]

Prelude> :type take take :: Int -> [a] -> [a]

For now you can view the function as ...

  • Having an argument of each type preceding the last ->
  • Having the return type following the last ->
  • Here: take receives an Int and [a] and returns an [a]

But why two -> (to) symbols?

  • -> always denotes a function that ...
    1. takes an argument of the type on the left and
    2. returns the type on the right
  • So here, the type on the right ([a] -> [a]) is a function!
    • That is, take 4 returns a function from a list of a to a list of a
  • -> is right-associative: a -> a -> a == a -> (a -> a)

Partial function application

Allows us to define partial applications of the function

Prelude> let take4 x = take 4 x

Prelude> :t take take4 :: [a] -> [a]

Prelude> take4 [1, 3 .. 21] [1, 3, 5, 7]

  • Where take and take4 have the types

take :: Int -> [a] -> [a] take4 :: [a] -> [a]

Excercise: Guess the types!

What are the types of these functions?

Prelude> null [5] null :: [a] -> Bool False

Prelude> lines "Hello\nWorld" lines :: String -> [String] ["Hello", "World"]

Prelude> take 5 [1..10] take :: Int -> [a] -> [a] [1, 2, 3, 4, 5]

Prelude> replicate 5 ’a’ replicate :: Int -> a -> [a] "aaaaa"

Good Practice to Specify Function Types

Although Haskell can infer them for you ...

  • you should define types for all your functions:

add :: (Num a) => a -> a -> a add x y = x + y

  • Haskell checks declared and inferred types match (early warning)
  • helps document your functions