14
Functional Programming Lecture 1 - Introduction Professor Muffy Calder

Functional Programming Lecture 1 - Introduction

Embed Size (px)

DESCRIPTION

Functional Programming Lecture 1 - Introduction. Professor Muffy Calder. About the Course. the Haskell functional language Lectures + Laboratories + Problem Sessions 2 Assignments Web site Lecture notes Assignments and problem sheets Resources Textbook - PowerPoint PPT Presentation

Citation preview

Page 1: Functional Programming Lecture 1 - Introduction

Functional ProgrammingLecture 1 - Introduction

Professor Muffy Calder

Page 2: Functional Programming Lecture 1 - Introduction

About the Course

• the Haskell functional language

• Lectures + Laboratories + Problem Sessions

• 2 Assignments

• Web site– Lecture notes

– Assignments and problem sheets

– Resources

• Textbook

Functional Programming in Haskell

by Simon Thompson

Page 3: Functional Programming Lecture 1 - Introduction

Programming Languages

• imperative: Ada, C, Pascal, …

• object: Java, C++, Smalltalk …

• logic: Prolog, ...

• functional: Haskell, ML, Lisp, Scheme, …– Haskell named after Haskell B. Curry

– Not Schoenfinkel

– Haskell is lazy

– ML is strict

Page 4: Functional Programming Lecture 1 - Introduction

What is Functional Programming?

• Based on mathematical functions

• Example– A function to compute whether or not an integer is in a list

of integers

inlist :: Int -> [Int] -> Bool

inlist x [] = False

inlist x (y:ys) = | (x == y) = True

| otherwise = inlist x ys

inlist 3 [5,3,6]

inlist 2 [5,3,6]

• Key concepts– Types

– Recursion

Page 5: Functional Programming Lecture 1 - Introduction

Why use Functional Languages?

• Concise and powerful style

• Rapid prototyping

• Strong error checking by intepreter/compiler

• Reduced debugging time

• Reliable, correct software

• Formal reasoning

• Why teach it in second year?– Good discipline + more abstract way of thinking

Page 6: Functional Programming Lecture 1 - Introduction

Haskell

• Standard functional language– developed by international committee

• 3 people from GU on that committee

• Used as the basis for numerous projects, in numerous countries

• Has technical characteristics that support software engineering and formal methods

Page 7: Functional Programming Lecture 1 - Introduction

Hugs

• An interactive interpreter for Haskell98– Hugs98 for windows

• Very quick ‘compilation’ and error checking

• Available on nearly all computers

• Free software

• See http://haskell.org/hugs

Page 8: Functional Programming Lecture 1 - Introduction

Overview of Functional Programming

2 slogans

• everything is a function

• every function has a type

FP consists of

• defining types

• defining functions

• applying functions and calculating the resulting expression

ftyped inputs

typedoutput

Page 9: Functional Programming Lecture 1 - Introduction

Types, Constants and Expressions

• type - set of possible valuesInt

• constant - a particular value in a type27:: Int

• expression - can be evaluated2+3*x

• notation: 2+2 => 4“2+2 evaluates to 4”

The type of + is Int->Int-Int, i.e._+_ :: Int->Int->Int

Page 10: Functional Programming Lecture 1 - Introduction

Int and Integer

• Int - 32-bit integers

• Integer - genuine integers

– no limit on size (except memory)

– all operations give correct result

– programmer doesn’t have to worry about how much memory to allocate

You will usually use Int.

Page 11: Functional Programming Lecture 1 - Introduction

Float and Double

• Single and double precision floating point numbers

• Does not give exact real arithmetic - there can be roundoff errors

• It is unlikely that you will use these types in this course.

Page 12: Functional Programming Lecture 1 - Introduction

Char

• ASCII characters

• constant - surround with single quote ‘x‘ :: Char

• can compare characters

`c` < ‘Z’

• case conversion

– toUpper ‘ w ‘ => ‘ W ‘

– toLower ‘Q ‘ => ‘ q ‘

– toUpper :: Char -> Char

Page 13: Functional Programming Lecture 1 - Introduction

Bool

• Boolean values, used to control conditionals

• only two constants: False :: Bool and

True :: Bool

• b && c (conjunction)

• b || c (disjunction)

• not b (negation)

Example:

( 3 <= x) && (x <= 10) :: Bool

Page 14: Functional Programming Lecture 1 - Introduction

Function application

• Write the name of the function followed by the arguments

• Sometimes use terminology rator and rand

• Don’t put parentheses around the argument

sqrt 9.0

fcn 2 8 13

3.4 + (sqrt x) * 100

2 * (sqrt (pi*r*r)) + y

E.g. In Maths f(x)

In FP (f x)