Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functional Programming Lecture 1 - Introduction Professor Muffy Calder.

Similar presentations


Presentation on theme: "Functional Programming Lecture 1 - Introduction Professor Muffy Calder."— Presentation transcript:

1 Functional Programming Lecture 1 - Introduction Professor Muffy Calder

2 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

3 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

4 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

5 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

6 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

7 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

8 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 f typed inputs typed output

9 Types, Constants and Expressions type - set of possible values Int constant - a particular value in a type 27:: Int expression - can be evaluated 2+3*x notation: 2+2 => 4 “2+2 evaluates to 4” The type of + is Int->Int-Int, i.e. _+_ :: Int->Int->Int

10 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.

11 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.

12 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

13 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

14 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)


Download ppt "Functional Programming Lecture 1 - Introduction Professor Muffy Calder."

Similar presentations


Ads by Google