Download Programming Paradigms - Universitatea Tehnica din Cluj

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

Flow-based programming wikipedia , lookup

Scala (programming language) wikipedia , lookup

Stream processing wikipedia , lookup

Join-pattern wikipedia , lookup

Class (computer programming) wikipedia , lookup

Go (programming language) wikipedia , lookup

Corecursion wikipedia , lookup

Common Lisp wikipedia , lookup

Lisp (programming language) wikipedia , lookup

Logic programming wikipedia , lookup

C++ wikipedia , lookup

Control flow wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

Programming language wikipedia , lookup

Falcon (programming language) wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Reactive programming wikipedia , lookup

Object-oriented programming wikipedia , lookup

Structured programming wikipedia , lookup

Functional programming wikipedia , lookup

Transcript
Programming Paradigms
Adrian Groza
Department of Computer Science
Technical University of Cluj-Napoca
Class Organisation
Programming Paradigms
Outline
1
Class Organisation
2
Programming Paradigms
Imperative programming
Declarative programming
3
Functional Languages
LISP
Haskel and ML
4
Food for Thought
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
General Info
Web page
http://cs-gw.utcluj.ro/∼adrian/teaching/pf.html
Contact
email: [email protected]
room 21, Baritiu
Lectures notes
slides available on the course web page
Ioan Alfred Letia, Programare Functionala, Editura
Tehnica, 1998
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Course Character
Prerequisites
basic notions of programming and data structures,
recursion
Course continually being improved
feedback is helpful
exercises during classes: extra points
Technical parameters
4 credits
course grade based on written exam (60%), lab
assignment (40%)
Food for Thought
Class Organisation
Programming Paradigms
The goal of the laboratory...
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Laboratory Work
1
Week 1: Lisp objects, forms evaluation, primitive functions
2
Week 2: Intern representation, evaluation control, functions
3
Week 3: Recursion
4
Week 4: Iteration
5
Week 5: Lambda expresions, high order functions, mapping
6
Week 6: Association lists, properties, arrays, structures
7
Week 7: Trees
8
Week 8: Graphs
9
Week 9: First Lab Assessment
10
Week 10-13: ML or LISP miniproject
11
Week 14: Second Lab Assessment
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Subjects covered
1
Programming paradigms
Functional languages charateristics
Functional programming languages
Functional programming applications
2
Lists
Fundamental operations on lists, Recursion, Matrix
operations, Association lists, List operators
3
Trees
Alternative data, Pattern matching, Binary trees
4
High order functions
Anonime functions, Functions as data, Mapping
5
Transformation and reasoning
Structural induction, Function equivalence, Specification
and verification
6
Lambda calculus
Class Organisation
Programming Paradigms
Outline
1
Class Organisation
2
Programming Paradigms
Imperative programming
Declarative programming
3
Functional Languages
LISP
Haskel and ML
4
Food for Thought
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Programming paradigms
Thesis (arguable)
1
Computer science is not a science (?)
A science makes experiments to find out how the reality
really is.
Computer science only invents new things (extends the
reality)
2
Programming is easy (?)
Programming languages for children of 12 years (Alice)
Programming is easy as long the project is small enough to
be contained in the working memory of the programmer
Programming is difficult when working at a part of the
project one does not know what other part contains
Our goal (as computer scientists
Learn how to deal with this complexity
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Handling the complexity
The recepy
1
Divide the problem in small pieces
2
Solve each small piece
3
If everything was done properly, we don’t break our legs
4
To avoid breaking legs ⇒ Programming Paradigms
Definition (Programming Paradigms)
Set of rules which should follow to program in a specific style.
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Programming Paradigms
Imperative Programming
Procedural programming
Object oriented programming
Aspect oriented programming
Context oriented programming
Declarative Programming
Functional programming
Logic programming
Constraints-based programming
Observation
There is no perfect mapping between a programming language
and the corresponding paradigm.
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Imperative programming
Procedural Programming
Program = Algorithm + Data structure
Computations are oriented towards data
modification in memory
”The most important part of writing a
program is designing the data structures.
The second most important part is
breaking the various code pieces down.”
Wirth’s law: ”Software is getting slower more
rapidly than hardware becomes faster.”
NiklausWirth
Bill Gates
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Imperative programming
Object Oriented Programming
root in software engineering and knowledge
representation based on frames from artificial
intelligence
reuse, extension, and easy modification of applications
lack of a formal (mathematical) model for automatic
verification of bugs
Class Organisation
Programming Paradigms
Functional Languages
Declarative programming
Logic Programming
Program = Logic + Control
declare what should be solve and not how
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Declarative programming
Constraints Programming
”Constraints programming represents one of
the closest approaches computer science has
made to the Holy Grail of programming: the
user states the problem, the computer solves it.”
Food for Thought
Class Organisation
Declarative programming
Is it hard?
Programming Paradigms
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Declarative programming
Constraints everywhere...
Combination
Simplification
Contradiction
Redundancy
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Declarative programming
Functional Programming
Aims to minimise side effects
Definition
The action throgh which a function, in addition to producing a value, modifies
the data structures from memory is called side effect
Example
Modification of a global or static variable, writing to a file or printing on the
display, reading data from functions with side effects
Functional programs are based on value return
A functional code looks fluid, an imperative code looks solid
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Declarative programming
Functional Programming
Program = Composition of Functions
f ◦ g ◦ h ◦ ...
Functions are first order constructs
arguments for other functions
output values of other functions
assigned value to a variable
created or modificated during the execution of the program
Functional programming languages
Haskel, ML, F], XSLT, LISP, Erlang
Class Organisation
Programming Paradigms
Functional Languages
Declarative programming
Good programmer
Should learn at least five languages
1
class-based (Java, C++)
2
procedural (C)
3
functional (ML or Lisp)
4
logic (Prolog)
5
parallel programming (Parlog)
Food for Thought
Class Organisation
Programming Paradigms
Outline
1
Class Organisation
2
Programming Paradigms
Imperative programming
Declarative programming
3
Functional Languages
LISP
Haskel and ML
4
Food for Thought
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
LISP Language
1960, MIT, John McCarthy
minimum set of operators: atom, eq, cons, car, cdr, cond,
qoute
notation for functions: LAMBDA calculus
=⇒ high level language
LISP = List Processing
Programming languages a la Darwin
Class Organisation
Programming Paradigms
LISP
TIOBE Index for September
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
LISP
Long term trends
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
LISP
Categories of Programming Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
Representative LISP-based applications
play
pause
resume
stop
Deep Space 1: Mars Pathfinder
Remote agent - Common Lisp
Formal verification with SPIN (System Software Award
2001)
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
Representative LISP-based applications
VIAWEB - 49 milion (1999)
”The more of an IT flavor the job descriptions had, the
less dangerous the company was. The safest kind were the
ones that wanted Oracle experience. You never had to worry
about those. You were also safe if they said they wanted C++
or Java developers. If they wanted Perl or Python
programmers, that would be a bit frightening– that’s starting
Paul
to sound like a company where the technical side, at least, is
Graham
run by real hackers. If I had ever seen a job posting looking
for Lisp hackers, I would have been really worried”.
Class Organisation
Programming Paradigms
Functional Languages
LISP
Representative LISP-based applications
AUTOCAD
AutoLisp and VisualLisp dialects
play
pause
resume
stop
Emacs
scripts for automated editing
Lisp and Semantic web
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
LISP
Representative LISP-based applications
”The Semantic Web is not a separate
Web but an extension of the current one,
in which information is given well-defined
meaning, better enabling computers and
people to work in cooperation.”
Food for Thought
How the future will look like?
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
Semantic Web
Social Web
Semantic Web
Lisp-based tools suporting knowledge representation and
reasoning in the Semantic Web
Loom, Racer, AllegroGraph
The Use of Lisp in Semantic Web Applications in
Proceedings of International Lisp Conference 2003, New
York, Katz, Yarden and Hendler, James, August 2003.
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
LISP - succesful domains
Artificial Intelligence
Graphics and animation
Bioinformatics
E-commerce
Multiagent systems
NLP
Modelling and Simulation
Planning
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
Why is Lisp special?
1
2
3
4
5
6
Conditionals - invented by McCarthy in the course of
developing Lisp.
A function type - functions are data type just like integers,
strings, can be stored in variables, can be passed as
arguments.
Recursion - Lisp was the first programming language to
support it.
A new concept of variables - All variables are effectively
pointers. Values are what have types, not variables, and
assigning or binding variables means copying pointers, not
what they point to.
Garbage-collector.
The whole language always available - There is no real
distinction between read-time, compile-time, and runtime.
You can compile or run code while reading, read or run
code while compiling, and read or compile code at runtime.
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
New language = C model + Lisp feature
”We were not out to win over the
Lisp programmers; we were after
the C++ programmers. We
managed to drag a lot of them
about halfway to Lisp.”
Guy Steele, Java co-author
”The primary goal of LINQ is to
unify programming against
relational data, objects, and XML”.
Anders Hejlsberg, Chief
Architect of C] and Delphy
Class Organisation
Programming Paradigms
LISP
When to use Lisp?
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
When to use Lisp?
1
Exploratory projects
Use top down except for the first time
2
3
Projects with succesive interactions with the customers
Rapid prototyping
compilation no needed
unit testing
4
Minimising delivering time
Class Organisation
Programming Paradigms
Functional Languages
LISP
When to use Lisp?
Types of applications
1
Small projects
2
Translation applications
3
Extendable applications
Issues when using a less spread language
1
Interacting with other applications
2
Few libraries
3
Few programmers
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
How to became a hacker I
”If you don’t know any computer languages, I
recommend starting with Python. It is cleanly
designed, well documented, and relatively kind to
beginners. Despite being a good first language, it is
not just a toy; it is very powerful and flexible and well
suited for large projects.[..]. I used to recommend Java
as a good language to learn early.”
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
LISP
How to became a hacker II
”If you get into serious programming, you will have to
learn C, the core language of Unix. And, actually, the
more you can avoid programming in C the more
productive you will be. Perl is worth learning for
practical reasons; it’s very widely used for active web
pages and system administration.”
”LISP is worth learning for a different reason - the
profound enlightenment experience you will have
when you finally get it. That experience will make you
a better programmer for the rest of your days, even if
you never actually use LISP itself a lot.”
Class Organisation
Programming Paradigms
Functional Languages
LISP
How to became a hacker III
”It’s best, actually, to learn all five of Python, C/C++,
Java, Perl, and LISP. Besides being the most
important hacking languages, they represent very
different approaches to programming, and each will
educate you in valuable ways.”
Food for Thought
Class Organisation
Programming Paradigms
LISP
A short trip to LISP
Lisp features
1
2
3
Interactivity
Atoms and lists
Function composition
Basic Lisp functions
1
2
3
4
5
6
Arithmetic and Logic
Selectors
Constructors
Binding
Predicates
Input/Output
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Haskel and ML
Haskell
Haskell 98 stable version
Pure functional language ⇔ no side effects
persistent data (immutable) - once loaded,
they cannot be changed
lazy evaluation - when needed
Rufl, Hugs (Haskell User’s Gofer System)
Hugs is the most used Haskell system
(www.haskell.org/hugs)
prelude.hs provides basic functions
Haskell Curry
1900-1982
Class Organisation
Programming Paradigms
Haskel and ML
A short trip to HASKELL
.2+3*4
.(2+3)*4
.head [1,2,3,4,5]
.tail [1,2,3,4,5]
. take 3 [1,2,3,4,5]
. drop 3 [1,2,3,4,5]
. length [1,2,3,4,5]
. sum [1,2,3,4,5]
. product [1,2,3,4,5]
. [1,2,3] ++ [4,5]
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Haskel and ML
ML
Standard ML 97 - stable version
Features
Type inference - data types deduced by the
compilator
Pattern matching
Call by value
Standard ML, OCAML, F]
Food for Thought
Class Organisation
Programming Paradigms
Outline
1
Class Organisation
2
Programming Paradigms
Imperative programming
Declarative programming
3
Functional Languages
LISP
Haskel and ML
4
Food for Thought
Functional Languages
Food for Thought
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
”Lisp is worth learning for the profound enlightenment
experience you will have when you finally get it; that
experience will make you a better programmer for the
rest of your days, even if you never actually use Lisp
itself a lot.” (Eric Raymond, How to became a hacker).
”I have heard more than one LISP advocate state
such subjective comments as, ”LISP is the most
powerful and elegant programming language in the
world” and expect such comments to be taken as
objective truth. I have never heard a Java, C++, C,
Perl, or Python advocate make the same claim about
their own language of choice.” (A blogger)
Class Organisation
Programming Paradigms
Functional Languages
Food for Thought
Study ideas
1
Common Lisp Standard:
http://www.lispworks.com/reference/HyperSpec/Front/index.htm
2
Allegro Lisp, with references to commercial applications:
http://www.franz.com
3
Paul Graham http://www.paulgraham.com/ and Peter
Norvig http://norvig.com
Class Organisation
Programming Paradigms
Functional Languages
Bibliography
Mandatory
1
Ioan Alfred Letia, Programare
Functionala, UTPres, Cluj-Napoca,
1996
2
I.A. Letia, E.St. Chifu, C. Cenan,
Programare functionala. Indrumator de
laborator, Casa cartii de stiinta,
Cluj-Napoca, Romania, 1999.
Food for Thought