Docsity

Haskell Programming: Characteristics, Data Declarations, Functions, and Tuples, Study notes of Computer Science

Partial preview of the text

Download Haskell Programming: Characteristics, Data Declarations, Functions, and Tuples and more Study notes Computer Science in PDF only on Docsity!

Advanced Programming

Andrew Black and Tim Sheard Lecture 2 Intro to Haskell

Course URLs

  • http://web.cecs.pdx.edu/~cs410aph
    • Course web page
    • Links to lecture notes and assignments
  • http://samla.pdx.edu/portal
    • Homework submissions
    • Class mailing list.

Haskell

  • Haskell is a pure functional language.
    • Yet it supports imperative programming with the use of monads
  • There are several implementations
    • HUGS - http://haskell.org/hugs/
    • GHC - http://www.haskell.org/ghc/
  • Haskell has a sophisticated type system that catches many errors, and supports advanced abstraction mechanisms

Characteristics of Haskell

  • Applicative style
    • input output description of problem.
  • First class functions
    • pass as parameters
    • return as value of a function
    • store in data-structures
  • Less Importantly:
    • Automatic memory management (G.C. no new or malloc)
    • Use of a strong type system which uses type inference, i.e. no declarations but still strongly typed.

GHCI the GHC interactive system

___ ___ _ / _ \ /\ // () / /// // / / | | GHC Interactive, version 6.4.1, for Haskell 98. / /\/ __ / /| | http://www.haskell.org/ghc/ _// //__/|| Type :? for help. Loading package base-1.0 ... linking ... done. Prelude> length [1,2,3,4] 4 Prelude> In addition to the command-line compiler, GHC also comes with an interactive environment, GHCI, it works much like Hugs (after which it was modeled). Try it out!

The Hugs Interpreter

**__ __ __ __ ____ ___


|| || || || || || ||__ Hugs 98: Based Haskell 98 standard |||| |||| ||_|| __|| Copyright (c) 1994- ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Report bugs to: [email protected] || || Version: Nov 2003


Hugs mode: Restart with command line option +98 for Haskell 98 mode Hugs session for: C:\Program Files\Hugs98/libraries\Hugs\Prelude.hs C:\Program Files\Hugs98/libraries\Prelude.hs D:\work\sheard\Courses\CSE502\Fall2004\lect01.html Type :? for help Main>** Hugs Demo

Basic Approach

  • The basic approach to programming in Haskell is two fold - Define some data structures - Define functions over those data structures
  • In contrast with Smalltalk, where the model is simulation of the real world, the model is transformative. I.e. we think of transforming data from one form to another.

IntLists

data IntList = Cons Int IntList | Nil len Nil = 0 len (Cons x xs) = 1 + len xs addAll Nil = 0 addAll (Cons x xs) = x + addAll xs Define some data Write some functions

Constructing Data

  • Tree-like structures are created using constructor functions. - These have the same name as the tags - These names always start with an uppercase letter data IntList = Cons Int IntList | Nil (Cons 3 (Cons 2 (Cons 9 Nil))) 3 Cons Nil 2 Cons 9 Cons

Exercise

  • Define a Haskell Data declaration to capture the following tree-like structure. Data Tree1 = … 9 Fork 9 Fork 9 Fork Empty Empty Empty Empty

Exercise

Node Node Node 3 Tip 6 Tip 14 Tip 8 Tip

  1. Define a Haskell Data declaration to capture the following tree-like structure.
  2. Construct the tree

Functions

  • Functions are defined by cases over the data using patterns.
  • Patterns appear on the left-hand-side of a case.
  • Patterns match tree-like structures and bind variables to values. len Nil = 1 len (Cons x xs) = 1 + len xs

Pattern Matching

  • Every pattern matches and binds variables.
  • A pattern can match many structures.
  • E.g. ( Cons x xs ) and Nil 5 Cons Nil Nil x binds to 5 xs binds to Nil

Nested patterns an constants

  • ( Cons x (Cons 5 xs )) matches
  • But NOT 3 Cons Nil 5 Cons 9 Cons 3 Cons Nil 5 Cons Nil 4 Cons 22 Cons 22 Cons 5 Cons Nil (Cons 22 (Cons 5 Nil)) (Cons 3 (Cons 5 (Cons 9 Nil))) (Cons 5 (Cons 4 (Cons 22 Nil))) (Cons 3 Nil)