Download Introduction to Functional Programming (1)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Closure (computer programming) wikipedia , lookup

Anonymous function wikipedia , lookup

Standard ML wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Lambda calculus definition wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Combinatory logic wikipedia , lookup

Transcript
Introduction
First Steps
Introduction to Functional Programming (1)
– Language for Calculational Programming –
Zhenjiang Hu
SOKENDAI/National Institute of Informatics
Email: [email protected]
URL: http://research.nii.ac.jp/˜ hu
Sokendai, 2015
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
The Software Crisis
How can we cope with the size and complexity of modern
computer programs?
How can we reduce the time and cost of program
development?
How can we increase our confidence that the finished
programs work correctly?
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Language-based Approach
One approach to the software crisis is to design a good
programming language that:
Allow programs to be written clearly, concisely, and at a
high-level of abstraction;
Support reusable software components;
Encourage the use of formal verification;
Permit rapid prototyping;
Provide powerful problem-solving tools;
Suitable to be optimized, procedurally and algorithmically.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Language-based Approach
One approach to the software crisis is to design a good
programming language that:
Allow programs to be written clearly, concisely, and at a
high-level of abstraction;
Support reusable software components;
Encourage the use of formal verification;
Permit rapid prototyping;
Provide powerful problem-solving tools;
Suitable to be optimized, procedurally and algorithmically.
⇑
Functional languages provide an elegant framework
to address these goals.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
What is a Functional Language?
Functional programming is style of programming in which the
basic method of computation is the application of functions to
arguments;
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
What is a Functional Language?
Functional programming is style of programming in which the
basic method of computation is the application of functions to
arguments;
A functional language is one that supports and encourages the
functional style.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
An Example
Summing the integers from 1 to 10.
In Java: Computation method is variable assignment
total = 0;
for (i = 1; i ≤ 10; i++)
total = total + i;
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
An Example
Summing the integers from 1 to 10.
In Java: Computation method is variable assignment
total = 0;
for (i = 1; i ≤ 10; i++)
total = total + i;
In Haskell: Computation method is function application
sum [1..10]
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
1960s Peter Landin develops ISWIM, the first pure functional language, based
strongly on the lambda calculus, with no assignments.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
1960s Peter Landin develops ISWIM, the first pure functional language, based
strongly on the lambda calculus, with no assignments.
1970s John Backus develops FP, a functional language that emphasizes
higher-order functions and reasoning about programs.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
1960s Peter Landin develops ISWIM, the first pure functional language, based
strongly on the lambda calculus, with no assignments.
1970s John Backus develops FP, a functional language that emphasizes
higher-order functions and reasoning about programs.
1970s Robin Milner and others develop ML, the first modern functional
language, which introduced type inference and polymorphic types.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
1960s Peter Landin develops ISWIM, the first pure functional language, based
strongly on the lambda calculus, with no assignments.
1970s John Backus develops FP, a functional language that emphasizes
higher-order functions and reasoning about programs.
1970s Robin Milner and others develop ML, the first modern functional
language, which introduced type inference and polymorphic types.
1970s-80s David Turner develops a number of lazy functional languages,
culminating in the Miranda system.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Historical Background
1930s Alonzo Church develops the lambda calculus, a simple but powerful
theory of functions.
1950s John McCarthy develops Lisp, the first functional language, with some
influences from the lambda calculus, but retaining variable assignments.
1960s Peter Landin develops ISWIM, the first pure functional language, based
strongly on the lambda calculus, with no assignments.
1970s John Backus develops FP, a functional language that emphasizes
higher-order functions and reasoning about programs.
1970s Robin Milner and others develop ML, the first modern functional
language, which introduced type inference and polymorphic types.
1970s-80s David Turner develops a number of lazy functional languages,
culminating in the Miranda system.
1987 An international committee of researchers initiates the development of
Haskell, a standard lazy pure functional language.
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
A Taste of Haskell
Can you guess what the following Haskell program does?
f [] = []
f (x:xs) = f ys ++ [x] ++ y zs
where
ys = [a | a <- xs, a<=x]
zs = [b | b <- xs, b>x]
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
The GHC System
GHC is a state-of-the-art, open source, compiler for the
functional language Haskell.
GHCi is GHC’s interactive environment. Its interactive nature
makes it well suited for teaching and prototyping.
GHC/GHCi is available on the web from
http://www.haskell.org/ghc/
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Starting GHCi
On a Unix system, GHCi can be started by simply typing ghci.
Start GHCi
~/top/prof/teach/teach03/out-lectures/PKU12/slides> ghci
GHCi, version 7.0.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude>
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
The Prelude> means that the GHCi system is ready to evaluate
an expression (like a calculator).
Expression Evaluation
Prelude> 2+3*4
14
Prelude> (2+3)*4
20
Prelude> sqrt (3^2 + 4^2)
5.0
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
The Standard Prelude
The library file Prelude.hs provides a large number of standard
functions. In addition to the familiar numeric functions such as +
and ∗, the library also provides many useful functions on lists.
Prelude> head [1,2,3,4,5]
1
Prelude> tail [1,2,3,4,5]
[2,3,4,5]
Prelude> take 3 [1,2,3,4,5]
[1,2,3]
Prelude> drop 3 [1,2,3,4,5]
[4,5]
Prelude> length [1,2,3,4,5]
5
Prelude> sum [1,2,3,4,5]
15
Prelude> [1,2,3,4,5] ++ [6,7,8]
[1,2,3,4,5,6,7,8]
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Function Application
In mathematics, function application is denoted using parentheses,
and multiplication is often denoted using juxtaposition or space.
f(a,b) + c d
In Haskell, function application is denoted using space, and
multiplication is denoted using *.
f a b + c*d
And function application has higher priority than all other
operators.
f a + b = (f a) + b
Zhenjiang Hu
Introduction to Functional Programming (1)
Introduction
First Steps
Haskell Scripts – Defining New Functions
New functions are defined within a script, a text file comprising a
sequence of definitions.
test.hs
double x = x + x
quadruple x = double (double x)
GHCi: Load New Functions
Prelude> :load test.hs
[1 of 1] Compiling Main
Ok, modules loaded: Main.
*Main> quadruple 10
40
Zhenjiang Hu
( test.hs, interpreted )
Introduction to Functional Programming (1)
Introduction
First Steps
Exercise 1-1
Show how the following two functions can be defined using the
functions introduced in this lecture, and test them with GHCi.
last: selects the last element of a list.
average: computes the average of all numbers in a list.
Zhenjiang Hu
Introduction to Functional Programming (1)