PowerPoint Presentation
CS 345: Programming Language Paradigms
Today
“Paradigms”
What is functional programming?
Contrast: Imperative programming
Intro to functional programming in Racket
Expressions and expression evaluation
Defining simple functions
Heads up: Participation Quiz
“Paradigm,” in the context of programming languages:
A pattern that serves as a school of thought for programming of computers
A programming technique
All the ways a language supports this technique
An ideal. (Sometimes, programming purely in one paradigm is not as practical as mixing approaches)
Imperative Code Sample
Imperative Paradigm
Imperative programming is programming by means of changing state.
“State” means all the parts of a program that can change over the course of the program’s execution because they are assigned and re-assigned new values.
Formal Definition
Imperative languages are characterized as having state that is modified (“mutated” or “side-effected”) by commands in the source language. As a result, such languages generally have a notion of sequencing of commands to permit precise and deterministic control over the state. The assignment of state is a very common command, since its effect is to alter the underlying store.
Functional Programming
What’s the difference between an expression and a statement?
x + 7
x = x + 7;
Expression
7
x + 7
x * (y + 8)
myfunc(a)
A simple object, like a number or a named variable, or, an operator or a pure function applied to an argument or arguments
Always returns a new value
We say an expression is “evaluated”
Statement
X = 7
x = x + 7
print ”Hello world”
if x < 7 { x = 10 }
A command that carries out an action
A unit of execution that doesn’t return anything, rather only has a “side-effect” (has an observable interaction on the program’s state)
We say a statement is “executed”
Functional vs Imperative
Pure functional programming is programming exclusively with expressions
Imperative programming is programming with statements (which often include expressions)
Lisp
Scheme
Racket
Lisp
Invented by John McCarthy
(1927-2011) in 1958.
Used by artificial intelligence researchers.
Second oldest programming language.
“Functional” programming paradigm.
Simple syntax and semantics
Easy to parse and interpret
Easy to learn (although very deep)
Scheme
Dialect of Lisp
Elegant
Syntax
Smallness
Invented by Jerry Sussman and
Guy Steele in 1975
Racket
/docProps/thumbnail.jpeg