From Haskell: The craft of functional programming (3rd Ed ...

Topic 1: Introduction

Recommended Exercises and Readings

? From Haskell: The craft of functional programming (3rd Ed.)

? Readings:

? Chapter 1 ? Chapter 2

1

2

What is a Programming Paradigm?

? Programming Paradigm:

? A style or way of programming ? Influences how you solve problems and implement solutions

? Languages include features from one or more paradigms

3

Imperative Programming

? A program consists of a sequence of commands ? Commands describe how the computation takes place

? Step by step ? Modify state

? Most modern imperative languages are structured

? Conditionals, loops and subroutines (functions) instead of gotos ? Variables are generally local to a block or subroutine

4

Object Oriented Programming

? Program describes objects and the ways they interact

? Emphasizes objects instead of actions and data rather than logic

? Principal concern is sending messages to objects

? Objects respond to messages by performing operations ? Messages can have arguments

? Look like calling a subroutine

? Similar objects are abstracted into a class ? Similar classes grouped together into inheritance hierarchies ? Often include mechanisms for encapsulation and information hiding

5

Functional Programming

? Focus is on functions and the application of functions to values

? Evaluation of expressions rather than running commands

? Functions can be

? Constructed as a program runs ? Passed as parameters to other functions ? Returned as results from functions

? Pure functional languages

? Do not include loops ? Functions are side-effect free ? No (modifiable) global state

6

Logic Programming

? Program is a collection of facts and inference rules ? Identify goal ? Unification and backtracking find a value that satisfies the goal

? Occurs automatically ? programmer doesn't specify how to find the solution

Human Language

? In written form...

? Letters are used to form words ? Words are used to form phrases ? Phrases are used to form sentences ? Sentences are used to form paragraphs ? Paragraphs are used to form sections ? Sections are used to form chapters ? Chapters are used to form books

7

8

Human Language

? But...

? Not every sequence of letters is an "allowed" word ? Not every combination of words is an "allowed" phrase ? Not every combination of phrases is an "allowed" sentence ?...

? Syntax defines the structure of what is allowed

? Grammar describes syntax

? Humans attach meaning to words

? Semantics: The branch of linguistics and logic concerning meaning

Ambiguities in Language

Look at the bow!

9

10

Programming Languages

? Syntax: Rules that describe valid combinations of symbols and the structure of a program

? Semantics: Rules that describe the meaning of the statements

? Programming languages...

? Communicate instructions to the machine ? Can be written by a person ? Minimize ambiguity ? May be defined by

? Specification ? Implementation

So Many Programming Languages!

19

20

So Many Programming Languages!

Haskell

? Almost all programming languages are Turing complete

? With enough time and memory, they can compute anything that is computable

? Why are there so many programming languages?

? Programming languages evolve

? Learn from predecessors ? New problems may be easier to solve with new languages

? Different tools for different jobs

? Trade-offs between safety, speed and convenience

? Different people have different tastes and abilities

? Variety is beneficial!

? A functional programming language

? Originally developed in the 1990s

? Successor to Miranda

? General purpose ? "Pure" ? Offers several interesting features

? Strong type system with type inference and type classes ? Lazy evaluation ? Pattern matching ? List comprehensions

? Can be interpreted or compiled ? Popular in academia, and has seen some industrial use

21

22

Getting Haskell

? Download it for free...

? ? I used the "Haskell Platform" installer

? Available for Windows, Linux and MacOS ? Probably more than what you really need

? You'll also need your favourite editor

Writing our First Function

? To define a function in Haskell we need

? It's name ? It's type

? Types of parameters passed to the function ? Type of the value returned by the function

? Syntactic requirements:

? Function names must start with a lowercase letter ? Types start with uppercase letters ? Function name is separated from its type by ::

23

24

Functions: Definitions in Different Languages

? Define a function named alwaysFive which always returns the Int 5

25

Haskell Types

? Int

? Fixed Range ? -264 to 264 -1 on my system

Operation Addition Subtraction

Operator / Function + -

? Integer

? Arbitrarily large integers

? Convert to/from other numeric types with toInteger and fromInteger

Multiplication

*

Division (Truncated integer result) quot

Remainder

rem

Round to closest integer

round

Absolute value

abs

? Float

? Number with both an integer and fractional part ? Convert to/from other numeric types with toIntegral/fromIntegral

26

Haskell Types

? Boolean

? True or False ? Operators: &&, || and not

? Char

? Literals enclosed in single quotes

? String

? Literals enclosed in double quotes ? Equivalent to [Char]

? Char: 'a', String ['a'] or "a"

? Concatenate strings with ++

?...

Functions: Adding Parameters

? Define a function that returns twice it's Int parameter

27

28

Functions: Additional Parameters

? Define a fused-multiply-add function which computes a * b + c, where a, b and c are all of type Float

29

Calling Functions

? Brackets aren't needed for many function calls in Haskell

? Places where brackets are needed

? Negative numbers ? Grouping to avoid passing a function as one of the parameters

30

Summary

? Programming languages

? Four broad paradigms ? Some languages have features from multiple paradigms ? Provide a mechanism to communicate with the computer

? Haskell

? A functional programming language ? Function type declaration: name :: Type ? Function implementation: name args = body

31

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download