<<

What is a Programming Paradigm?

•• A fundamental style of programming ––TheThe paradigm of the Topic 1: Introduction determines how you will solve a problem

Programming Paradigms •• Languages fit one or more paradigms A First Look at Haskell

1 2

Programming Paradigms Programming Paradigms

•• •• Object Oriented Programming ––ConcentratesConcentrates on determining what sequence ––ConcentratesConcentrates on modeling the of commands is needed to reach a solution relationships within the program –– Collections of statements are grouped into –– New classes can be created by extending procedures existing classes using inheritance ––IncludesIncludes control constructs such as ––ProvidesProvides effective mechanisms for conditionals and loops encapsulation and information hiding

3 4 Programming Paradigms Programming Paradigms

•• •• ––EmphasizesEmphasizes the evaluation of expressions ––DescribesDescribes what the result should look like as rather than the execution of commands a set of attributes or constraints rather than –– New functions can be created at runtime the sequence of steps taken to compute the ––PurePure functional languages do not include result assignment statements or looping constructs ––InIn many ways, this is the complete opposite of normally present in imperative and object imperative programming oriented languages ––FunctionsFunctions do not have any side effects

5 6

Haskell Haskell vs. Java

•• A functional programming language •• Java: public static int myFunction () •• How do we declare a ? name :: Type •• Haskell: myFunction :: Int •• Name must start with a lowercase letter •• Types start with an uppercase letter

7 8 Adding the Function Bodies Adding a Parameter

•• Java: •• Java: public static int myFunction () { public static intdbl (int( intx) { return 5; return x * 2; }} }}

•• Haskell: •• Haskell: myFunction :: Int dbl :: Int -->> Int myFunction = 5 dbl x = x * 2

9 10

Adding a Second Parameter Comments

•• Java: •• Single line comments: p… … int sum (int x, int y) { ---- This is a comment return x + y; •• MultiMulti--lineline comments: }} {{-- A comment that •• Haskell: spans several sum :: Int -->> Int -->> Int lines sum x y = x + y --}}

11 12 Modules Haskell Types

•• The standard is referred to as •• IntInt:: Integer values from --21474836482147483648 to “Prelude” 2147483647 ––ContainsContains a variety of useful functions •• Char: ASCII characters such as ‘A’, ‘z’, ‘5’ –– Automatically included and ‘+’ ––mustmust be imported from the Char module •• Other modules can provide additional •• BoolBool:: logical values True and False functionality •• Many additional types are also available ––importimport ModuleName

13 14

Int Example Char Example

•• A fused multiply add performs a •• ASCII defines an integer that corresponds multiplication followed by an addition to each character ––resultresult = a * b + –– chr returns the character corresponding to an integer fma :: Int -->> Int -->> Int -->> Int –– ord returns the integer corresponding to a character

isA :: Char -->> Bool isA c = (c == ‘A’)

15 16 Bool Example Invoking Functions

•• Basic logic operators include: •• Functions are invoked using their name ––&&:&&: and ––ParameterParameter list is not enclosed in brackets or ––||:||: or separated by commas –– not: not –– Individual parameters may need to be enclosed in brackets ••ParametersParameters which are themselves function •• What if we want to create xorxor?? invocations generally need to be enclosed in xor :: Bool -->> Bool -->> Bool brackets xor a b = ••NegativeNegative numbers generally need brackets someFunc3 1 (otherFunc2 0 ((--4))4)) 21

17 18

Hugs Summary

•• Hugs is a Haskell available for •• Haskell is a functional programming Windows and Linux langauge ––FunctionsFunctions are defined using the notation name :: Type •• Free download at: http://www.haskell.org/hugs ––ImplementationImplementation is defined using name args = body ––BasicBasic types include Int, Char and Bool •• Also need an editor of your choice: •• Now we can create Haskell Notepad, Emacs, Vi, Pico, … programs and run them using Hugs

19 20 Recommended Readings

•• Chapter 1 •• Chapter 2 •• Chapter 3.1, 3.2

Recommended Exercises

•• Exercise 3.1, 3.4, 3.6, 3.7, 3.8 & 3.9

21