Overview of the Haskell 98 Programming Language - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Overview of the Haskell 98 Programming Language

Description:

All variables and functions start with a lowercase letter ... Prelude :load myFile.hs. Main functionF 1 2. 3. Inside Hugs Interpreter ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 30
Provided by: JayT4
Category:

less

Transcript and Presenter's Notes

Title: Overview of the Haskell 98 Programming Language


1
Overview of the Haskell 98 Programming Language
  • (Condensed from Haskell Document by Godisch and
    Sturm)

2
How to Run Haskell Programs
  • Use the Hugs interpreter downloaded from
    www.haskell.org/hugs/
  • At the command line, enter hugs
  • Haskell script files usually end with a ".hs"
    extension
  • -- indicates a single-line comment
  • - - enclose multi-line comments
  • ltcommandgt executes a Haskell command
  • All variables and functions start with a
    lowercase letter
  • All types and constructors start with an
    uppercase letter

3
Summary of Commands Used in Hugs
4
Built-in Haskell Data Types
  • Int
  • Integer
  • Float
  • Double
  • Bool
  • Char
  • String

5
Using the Data Type Int
myFile.hs
functionF Int -gt Int -gt Int functionF x y x
y
Inside Hugs Interpreter
Preludegt load myFile.hs Maingt functionF 1 2 3
6
Operators Available for Type Int
  • 5 3
  • 5 - 3
  • 5 3
  • 5 div 3 Integer division (backquotes)
  • div 5 3
  • 5 mod 2 Modulo division
  • mod 5 2
  • 5 3 3rd power of 5
  • abs (-5) Absolute value, parentheses are needed
  • Negate (-5) Negation of -5
  • -5 Negative 5

7
Using the Data Type Bool
  • Values of type Bool are True or FalsefunctionF
    Int -gt BoolfunctionF (gt 7)
  • Boolean Operators
  • True False logical AND
  • True False - logical OR
  • not True - logical negation
  • True False - equality
  • True / False - inequality

8
Using the Data Type Char
  • A character is enclosed in a single quotec
    Charc 'a'
  • A character can be converted to its ASCII integer
    value and vice versa using predefined
    functionsgt ord 'a'97gt chr 97'a'

9
Using the Data Type String
  • A string is a (possibly empty) sequence of
    characters and is enclosed in double quotes
  • It is type synonym for a list of type
    CharfunctionS StringfunctionS "A string
    example"

10
Guards and Pattern Matching
functionG Int -gt Int functionG n n 0
0 otherwise n - 1
functionH Int -gt Int functionH 0
0 functionH (n 1) n
11
Precedence and Binding
  • Function application has the highest precedence
  • Example functionF n 1
  • Correct binding (functionF n) 1
  • Wrong binding functionF (n 1)

12
Indentation and the Offside Rule
  • Haskell reduces the need for parentheses to a
    minimum by using the offside rule
  • To detect the start of another function
    definition, Haskell looks for the first piece of
    text that lies at the same indentation or to the
    left of the start of the current function
  • Also, a semicolon can be used to separate several
    definitions on the same line

13
where and let keywords
  • Each expression may be followed by a local
    definitions using the keyword where
  • A similar approach can be done using the keywords
    let and in

functionF Int -gt Int functionF x g (x 1)
where g (2) functionM Int -gt
Int functionM x let functionP (2) in
functionP (x 1)
14
Tuples
  • A tuple is a grouping of two or more possibly
    different types representing a record or
    structure
  • person (String, Int)person ("Bill", 24)
  • A type synonym using the keyword type can be used
    to give an alternative name to a typetype
    Person (String, Int)

15
Polymorphic Functions
  • A polymorphic function has one definition. It
    accepts variables of different types as input at
    runtime
  • Polymorphic types begin with a lowercase
    letterfunctionF a -gt b -gt c -gt afunctionF x
    _ _ x
  • This is different from overloaded functions
  • They have many different definitions
  • Example is the equality function, which is
    defined differently for type Int compared to type
    Bool

16
Type Classes
  • Members of a type classe are called instances
  • Predefined instances of class Eq are Int, Float,
    Double, Bool, Char, and String
  • Haskell has other built-in classes
  • Eq - equality and inequality
  • Ord - ordering over elements of a type
  • Enum - enumeration of a type
  • Show - conversion of the elements of a type into
    text
  • Read - conversion of values to a type from a
    string

17
Function Composition
  • The function composition operator is the dot (.)
  • It type of the dot is (b -gt c) -gt (a -gt b) -gt
    (a -gt c)
  • ExamplefunctionF Char -gt CharfunctionF c
    chr (succ (ord c))functionG Char -gt
    CharfunctionG chr . succ . ord

18
Using Lists
  • A list is an ordered set of values of the same
    type
  • It is enclosed in brackets with each element
    separated by a comma

listOfInt Int listOfInt 1,2,3,4 listOfCh
ar Char listOfChar 'h', 'e', 'l', 'l',
'o' listOfString Char listOfString
'o', 'n', 'e', "two", "three"
19
Construction of Lists
  • A list can be written as xxs, where x is the
    head of the list, xs is the tail, and is the
    cons operator
  • Every list is built up recursively using the cons
    operator whose type is a -gt a -gt a
    1 121 2, 132,1
    321 3, 2, 1
  • The list concatenation operator is and its
    type is a -gt a -gt a1,2
    3,4,5 1, 2, 3, 4, 5
  • Lists of elements can be denoted by giving a
    range2 .. 8 2, 3, 4, 5, 6, 7, 81, 3
    .. 12 1, 3, 5, 7, 9, 11

20
Pattern Matching Over Lists
  • An unstructured variable xs matches any list
  • (__) matches any non-empty list without binding
    of list elements
  • (xxs) matches any non-empty list however, x is
    bound to the head and xs to the tail
  • (_) is identical to _ and matches any list
    with one and only one element
  • (x1x2xs) extracts the first two elements of a
    list containing two or more elements, with x1 and
    x2 bound to the first two elements and xs bound
    to the tail

21
List Comprehension
  • A subset of a list can be generated using list
    comprehension in a common mathematical set
    notationx xlt-g, even x generator
    guard(Translated as x such that x is a
    member of the list g and x is even)
  • Generate a list of all possible pairs out of two
    setspairs a -gt b -gt (a,b)pairs xs ys
    (x,y) x lt- xs, y lt- ys

22
Recursion over Lists
  • Example Sum up the elements of a listsum
    Int -gt Intsum 0sum (xxs) x sum xs
  • Example Filter a list according to a selection
    criteriafilter (a -gt Bool) -gt a -gt
    afilter p filter p (xxs) p x
    x filter p xs otherwise
    filter p xs

Note sum and filter are both predefined
functions in Haskell
23
Some Predefined List Functions
  • () a -gt a -gt a
  • () a -gt a -gt a
  • (!!) a -gt Int -gt a
  • map (a-gtb) -gt a -gt b
  • filter (a -gt Bool) -gt a -gt a
  • head a -gt a
  • tail a -gt a
  • last a -gt a
  • init a -gt a
  • length a -gt Int
  • null a -gt Bool
  • take Int -gt a -gt a
  • drop Int -gt a -gt a

24
Algebraic Data Types Enumeration
  • This is the simplest kind of algebraic typedata
    Color Red Green Bluedata RoomNumber 102
    214 228 233 245assignColor
    RoomNumber -gt Color

25
Algebraic Data Types Product
  • This is used to combine two or more typestype
    Name Stringtype Age Intdata People
    Person Name Age
  • Person is referred to as the constructor of type
    People. Its type is Name -gt Age -gt People
  • Person is a function that generates an element of
    type People out of the types Name and Age

26
Algebraic Data Types Alternatives
  • This is used to combine two or more types but
    allows some choicestype Name Stringdata
    Year Freshman Sophomore
    Junior Seniordata Dept Bus Eng LA
    Math data People Student Name Year
    Staffer Name Dept

27
Algebraic Data Types Recursive
  • A recursive type can be created from an
    alternative typedata Tree Nil Node Int Tree
    TreemyTree TreemyTree Node 1 (Node 2
    (Node 4 Nil Nil) (Node 5 Nil Nil)) (Node
    3 Nil Nil)depth Tree -gt Intdepth Nil
    0depth (Node _ t1 t2) 1 max (depth
    t1) (depth t2)

28
Instances of Classes
  • When creating a new algebraic type, you may need
    to inherit operations from other type classes.
    This is done using the deriving keyworddata
    Color Red Green Blue deriving (Eq, Ord,
    Enum, Show, Read)

29
Example of an Abstract Data Type
Module Stack (Stack, isEmpty, push, pop)
where data Stack a Empty MyStack a (Stack
a)isEmpty Stack a -gt Bool isEmpty Empty
True isEmpty (MyStack _ _) False push
a -gt Stack a -gt Stack a push element aStack
MyStack element aStack pop Stack a -gt (a,
Stack a) pop Empty error "Empty
stack" pop (MyStack element aStack)
(element, aStack)
Write a Comment
User Comments (0)
About PowerShow.com