Docsity

Haskell Syntax & Key Concepts: Functions, Datatypes, Layout, Comments, Local Defs - Prof. , Study notes of Computer Science

Toggle sidebar
bg1
COSC 3015: Lecture 5
Lecture given by Prof. Caldwell and scribed by Sunil Kothari
September 16, 2008
1 Some Notes on Haskell Syntax
Function names, variable names starts with lowercase letter - followed by
zero or more digits, underscores ( ) upper or lowercase letters, forward
quotes (´
). For example, x1, x0, x1, x00.
Datatype and datatype constructor names start with uppercase letters
and are otherwise like functions and variable names. For example, Bool,
String, Int , True, False.
Keywords case, class, data, default, deriving, do, else, if, import, in , infix,
infixl, infixr, instance, let, module, newtype, of , then, type, where.
1.1 Layout rule
Each function definition must begin in the same column.
a=b+c
where
b=1
c=2
da=a*2
a=b+c
where
{b=1;c=2}
da=a*2
1.2 Comments
Line comments start with ”–” and continue to the end of the line. For exam-
ple,
fx =x+ 1 − −dumbfunction
Nested comments are of the form {− · · · −}
1
pf3
pf4

Partial preview of the text

Download Haskell Syntax & Key Concepts: Functions, Datatypes, Layout, Comments, Local Defs - Prof. and more Study notes Computer Science in PDF only on Docsity!

COSC 3015: Lecture 5

Lecture given by Prof. Caldwell and scribed by Sunil Kothari

September 16, 2008

1 Some Notes on Haskell Syntax

Function names, variable names starts with lowercase letter - followed by zero or more digits, underscores ( ) upper or lowercase letters, forward quotes (´). For example, x 1 , x′, x 1 , x′′.

Datatype and datatype constructor names start with uppercase letters and are otherwise like functions and variable names. For example, Bool, String, Int , True, False.

Keywords case, class, data, default, deriving, do, else, if, import, in , infix, infixl, infixr, instance, let, module, newtype, of , then, type, where.

1.1 Layout rule

Each function definition must begin in the same column.

a = b + c where b = 1 c = 2

d a = a* 2

a = b + c where {b = 1; c = 2}

d a = a* 2

1.2 Comments

Line comments start with ”–” and continue to the end of the line. For exam- ple, f x = x + 1 − −dumbf unction Nested comments are of the form {− · · · −}

1.3 Local Definition

The following code has the same effect as the ex. 1

a = let b = 1 c = 2 in b + c

Here’s another version of it.

a = b + c where b = 1 c = 2

Meaning of let-in. let x = t 1 in t 2 means t 2 [x := t 1 ] where,

  • x is locally defined variable
  • t 1 is local declaration of x
  • t 2 body of the let-declaration

and t 2 [x := t 1 ] - evaluate expression t 2 where all free occurrences of x get the value of expression t 1. Note that ”:=” is a capture-avoiding substitution

let x = 1 in let x = 2 in x

we can elaborate on it ∀x : Int, ∀x : nat.x ≥ 0. Choose arb. y ∈ int and show ∀x : Int, ∀x : nat.x ≥ 0[x := y] ; ∀x : nat, x ≥ 0

1.4 if-then-else

A code fragment in an imperative language

if x > 0 then y := x else y:= (-x)

In C and C++ there is a conditional expression

1.6 Infix operators

(&&),(||) :: Bool -> Bool -> Bool

True && x = x False && _ = False

True || x = True False || x = x

False && infinity

In Haskell, we have lazy evaluation so if we try to evaluate the above expression it evaluates to False. But in eager evaluation it will loop forever. There is a little bit of asymmetry here - Infinity and False will not terminate but evaluate to infinity.

not ::Bool -> Bool not True = false not False = True

otherwise::Bool not otherwise = True.