Docsity

Haskell Lecture Notes: Strings, Lists, Tuples, and Functions (Spring 2013), Study notes of Programming Languages

Partial preview of the text

Download Haskell Lecture Notes: Strings, Lists, Tuples, and Functions (Spring 2013) and more Study notes Programming Languages in PDF only on Docsity!

Today’s Topics ...

  • Strings
  • Some list functions
  • Tuples and functions
  • Defining functions

Strings

String values are surrounded by double quotes

Prelude> "Hello World!" "Hello World!"

  • String characters can be escaped with \n, \t, etc.
  • Characters are in single quotes

Prelude> ’a’ ’a’

Prelude> ’abc’ :1:2: lexical error in string/character literal at character ’b’

  • A string is defined in Haskell as a list of characters

Prelude> :type ’a’ ’a’ :: Char

Prelude> :type "abc" "abc" :: [Char]

  • This means we can use list operations with strings
  • Haskell also defines a String type as an alias for [Char]

List Functions

The Prelude defines many list functions

Two really useful ones are:

  • head (aka “car”) gives first value of a list

Prelude> head [4, 1, 5, 3] 4

Prelude> head [] *** Exception: Prelude.head: empty list

  • tail (aka “cdr”) gives list minus head value

Prelude> tail [4, 1, 5, 3] [1, 5, 3]

Prelude> tail [1] []

Prelude> tail [] *** Exception: Prelude.tail: empty list

  • null checks for the empty list

Prelude> null [] True

Prelude> null [1, 2] False

  • init gives list minus last value

Prelude> init [4, 1, 5, 3] [4, 1, 5]

Prelude> init [1] []

Prelude> init [] *** Exception: Prelude.init: empty list

  • reverse gives list reversed

Prelude> reverse [4, 1, 5, 3] [3, 5, 1, 4]

Prelude> reverse [1] [1]

Prelude> reverse [] []

  • elem x is true if x in list

Prelude> elem 1 [4, 1, 5, 3] True

Prelude> elem 0 [4, 1, 5, 3] False

Prelude> elem ’a’ [4, 1, 5, 3] :15:11: No instance for (Num Char) arising from the literal ‘4’ ...

  • notElem x is false if x in list

We’ll look at more later ...

Haskell Tuples

A “tuple” is a fixed size collection of values

  • Each tuple value can have a different type
    • Whereas each value of a list is of the same type
  • Tuple values and types are denoted using parentheses ...

Prelude> (1964, "Fist Full of Dollars") (1964, "Fist Full of Dollars")

Prelude> :type (True, "yes") (True, "yes") :: (Bool, [Char])

The special “unit” tuple

Prelude> () ()

Prelude> :type () () :: ()

  • Both a value and a type
  • Similar to void in C++/Java

There are no one-valued tuples in Haskell

Prelude> :type ("hello") ("hello") :: [Char]

Accessing Tuple Values

Getting data out of tuples is not so easy

  • For pairs, can use the functions fst and snd

Prelude> fst (1, True) 1

Prelude> snd (1, True) True

  • But this doesnt work for triples, 4-tuples, etc.

Prelude> fst (1, True, "hello")

:1:4: Couldn’t match expected type ‘(a, b)’ against inferred type ‘(t, Bool, [Char]) ...

We can define our own functions to do this ...

Defining Functions

Functions can be defined in ghci using let

Prelude> let f p 1 p 2... pn = e

  • f is the function name
  • p 1... pn are formal parameters (no commas)
  • e is an expression (i.e., evaluates to a value)

A simple example:

Prelude> let add x y = x + y

Usually more convenient to use source files

-- ex1.hs -- add function add x y = x + y

  • Source files can be loaded into ghci

Prelude> :load ex [1 of 1] Compiling Main ( example.hs, interpreted ) Ok, modules loaded: Main. *Main> add 3 4 7

  • By convention .hs used as file extension

Example: Define an avg3 function

  • should take three values and return their average

avg3 x y z = (x + y + z)/3 -- indentation required

Example: Accessing tuple elements

fst3 (x, y, z) = x

Prelude> fst3 (’a’, ’b’, ’c’) ’a’

snd3 (x, y, z) = y

Prelude> snd3 (’a’, ’b’, ’c’) ’b’

  • Note both fst3 and snd3 take one argument (a triple)
  • The argument is described as a “pattern”
  • That is, not just a named argument (the structure plus variables)
  • Haskell supports a rich “pattern matching” syntax

Example: listify

listify x = [x] -- e.g., listify 5 => [5]