22
CSEN403: Concepts of Programming Languages Functional Programming II: Types and Currying Nada Sharaf Spring Semester 2019 Nada Sharaf Functional Programming II Spring Semester 2019 1 / 22

CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

CSEN403: Concepts of Programming LanguagesFunctional Programming II: Types and Currying

Nada Sharaf

Spring Semester 2019

Nada Sharaf Functional Programming II Spring Semester 2019 1 / 22

Page 2: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Type System of Haskell

Haskell is able to infer the functions typeI i.e. if you omit type specifications for a function, Haskell will still be

able to infer the type of any well-formed expression.Haskell is strongly typed.

I Programs with inconsistent types are rejectedI Example:

F You can not apply a function to a value with a wrong typeF You cannot write a function that produces values of different type

classes

Nada Sharaf Functional Programming II Spring Semester 2019 2 / 22

Page 3: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

What is a type

A name for a group of related valuesTrue and False are of the same type: Bool

Nada Sharaf Functional Programming II Spring Semester 2019 3 / 22

Page 4: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Type Specification

e :: t

The expression e has a type tExamples:

I > :t TrueTrue :: Bool

I > :t ’a’’a’ :: Char

I > :t "Nada""Nada" :: String

Nada Sharaf Functional Programming II Spring Semester 2019 4 / 22

Page 5: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Type Classes

A class has different types inside it.=> is used to differentiate between the classes used and the actualtypesBefore the =>, constraints on types are places.After => actual type is placedExamples:

I > :t 11 :: Num a => a

1 is an instance of NumericI > :t (1,2)

(1,2) :: (Num a, Num b) => (b,a)

I > :t (1,2.5)(1,2.5) :: (Fractional a, Num b) => (b,a)

Nada Sharaf Functional Programming II Spring Semester 2019 5 / 22

Page 6: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Some Built-In Types

Integer unbounded integer numbersInt 32-bit integer numbers

Rational unbounded rational numbersFloat, Double single- and double-precision floating-point numbers

Bool Boolean values (True, False)Char characters, e.g., ‘a’

All types and type classes start with an upper case characterInt has a fixed capacity whereas Integer in theory has no bounds onsize

Nada Sharaf Functional Programming II Spring Semester 2019 6 / 22

Page 7: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Types for Functions

a->b

The input has type a

The output has type b

Example:> :t oddodd :: Integral a => a -> Bool

Example:> :t eveneven :: Integral a => a -> Bool

Nada Sharaf Functional Programming II Spring Semester 2019 7 / 22

Page 8: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Example

add (x,y) = x+y

> :t addadd :: (Int,Int) -> Int

If we want to restrict it to Int values onlyIn the file we have to restruct the type definition to overwrite theinferred one.add:: (Int,Int) -> Int

Nada Sharaf Functional Programming II Spring Semester 2019 8 / 22

Page 9: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Currying

add x y = x+y

You might believe that the function add has two inputs and oneoutput.This is not the case in Haskell.In Haskell, every function has one input and one outputInputs and outputs could howveer be in turn functionIn other words, the input of add is a numeric x

Its output is a function hence the term curried function.The output function in turn has one input y and an outputrepresenting the sum of x and y

Nada Sharaf Functional Programming II Spring Semester 2019 9 / 22

Page 10: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Currying Contd.

add x y = x+y

add:: Num a => a -> (a-> a)

The brackets are usually omitted

add:: Num a => a -> a-> a

Nada Sharaf Functional Programming II Spring Semester 2019 10 / 22

Page 11: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Polymorphic Functions

The type of function contains many (poly) types

> length [1,2,3]3

>length [1.5,6.2]2

>length [’a’,’b’,’c’,’d’]4

length :: [a] -> Int

Nada Sharaf Functional Programming II Spring Semester 2019 11 / 22

Page 12: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Defining New Names for Existing types

We use the keyword type

Type declarationtype MyString = [Char]

type Point = (Int, Int)

Nada Sharaf Functional Programming II Spring Semester 2019 12 / 22

Page 13: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Example

type Point = (Int, Int)

getX :: Point -> IntgetX (x,y) = x

Nada Sharaf Functional Programming II Spring Semester 2019 13 / 22

Page 14: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Type Declaration

Can have a parameter

type Point2 a = (a,a)

getX2 :: Point a -> agetX2 (x,y) = x

Nada Sharaf Functional Programming II Spring Semester 2019 14 / 22

Page 15: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

New Types

Use the keyword data.You start to enumerate the possible templates of the values of thistypedata Fruit = Apple | Banana

Without the new data Fruit introduced, we can not use the valuesApple or BananaApple and Banana are called the constructorsThey are used to construct values of the new type Fruit/

Nada Sharaf Functional Programming II Spring Semester 2019 15 / 22

Page 16: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Example

color :: Fruit -> Stringcolor Apple = "red"color Banana = "yellow"

Nada Sharaf Functional Programming II Spring Semester 2019 16 / 22

Page 17: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Parameters

A constructor can have parametersExampledata Shape = Circle Float | Rect Float Float

To construct a value of the type Shape, you canI Use the constructor Circle followed by an floating point value

(representing the radius)I Use the constructor Rect followed by two floating point values

(representing the length and width)

Nada Sharaf Functional Programming II Spring Semester 2019 17 / 22

Page 18: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Example

area (Circle r) = 3.14*r*rarea (Rect l w) = l*w

Nada Sharaf Functional Programming II Spring Semester 2019 18 / 22

Page 19: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Deriving Show

data Shape = Circle Float | Rect Float Float deriving Show

To be able show/print values of this new typeFunctions: formCircle, formRectformCircle r = Circle rformRect l w = Rect l w

Similarly, to be able to check if two values of a new type are equal ornot, the new data type has to be deriving Eq

Nada Sharaf Functional Programming II Spring Semester 2019 19 / 22

Page 20: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Define a list of integers

data List = Empty | L Int List deriving Show

Nada Sharaf Functional Programming II Spring Semester 2019 20 / 22

Page 21: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Define a list of any elements

data List a = Empty | L a (List a) deriving Show

Nada Sharaf Functional Programming II Spring Semester 2019 21 / 22

Page 22: CSEN403: ConceptsofProgrammingLanguages ...met.guc.edu.eg/Download.ashx?id=29861&file=Func_2_29861.pdfWhatisatype Anameforagroupofrelatedvalues True andFalse areofthesametype: Bool

Thank You!

Nada Sharaf Functional Programming II Spring Semester 2019 22 / 22