43
Functional Functional Programming Programming and Lisp and Lisp

Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Embed Size (px)

Citation preview

Page 1: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Functional Functional ProgramminProgramming and Lisp g and Lisp

Page 2: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

OverviewOverview

In a functional programming In a functional programming language, functions are first class language, functions are first class objects.objects.

You can create them, put them in You can create them, put them in data structures, compose them, data structures, compose them, specialize them, apply the to specialize them, apply the to arguments, etc.arguments, etc.

We’ll look at how functional We’ll look at how functional programming things are done in Lispprogramming things are done in Lisp

Page 3: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

evaleval

Remember: Lisp code is just an s-expressionRemember: Lisp code is just an s-expression You can call Lisp’s evaluation process with the You can call Lisp’s evaluation process with the

eval function.eval function. (define s (list cadr ’ ’(one two three)))(define s (list cadr ’ ’(one two three))) ss (CADR '(ONE TWO THREE))(CADR '(ONE TWO THREE))

> (eval s)> (eval s)

TWOTWO

> (eval (list cdr (car '((quote (a . b)) c))))> (eval (list cdr (car '((quote (a . b)) c))))

BB

Page 4: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

ApplyApply ApplyApply takes a function and a list of takes a function and a list of

arguments for it, and returns the result of arguments for it, and returns the result of applying the function to the arguments:applying the function to the arguments:

> (apply + ‘(1 2 3))> (apply + ‘(1 2 3))

66 It can be given any number of arguments, It can be given any number of arguments,

so long as the last is a list:so long as the last is a list:> (apply + 1 2 ‘(3 4 5))> (apply + 1 2 ‘(3 4 5))

1515 A simple version of apply could be written A simple version of apply could be written

asas(define (apply f list) (eval (cons f list)))(define (apply f list) (eval (cons f list)))

Page 5: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

LambdaLambda

The The definedefine special form creates a special form creates a function and gives it a name.function and gives it a name.

However, functions don’t have to However, functions don’t have to have names, and we don’t need have names, and we don’t need definedefine to define them. to define them.

We can refer to functions literally by We can refer to functions literally by using a using a lambda expressionlambda expression..

Page 6: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Lambda expressionLambda expressionA A lambda expression lambda expression is a list is a list containing the symbol containing the symbol lambdalambda, , followed by a list of followed by a list of parametersparameters, , followed by a followed by a bodybody of zero or more of zero or more expressions:expressions:

> (define f (lambda (x) (+ x 2)))> (define f (lambda (x) (+ x 2)))

> f > f

#<proceedure:f>#<proceedure:f>

> (f 100)> (f 100)

102102

Page 7: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

A lambda expression is a special A lambda expression is a special formform

When evaluated, it creates a When evaluated, it creates a function and returns a reference to function and returns a reference to itit

The function does not have a nameThe function does not have a name a lambda expression can be the a lambda expression can be the

first element of a function call:first element of a function call:> ( (lambda (x) (+ x 100)) 1)> ( (lambda (x) (+ x 100)) 1)101101

Other languages like python and Other languages like python and javascript have adopted the ideajavascript have adopted the idea

Lambda expressionLambda expression

Page 8: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

define vs. definedefine vs. define

(define (add2 x)(define (add2 x) (+ x 2) ) (+ x 2) )

(define add2(define add2(lambda (x) (+ x 2)))(lambda (x) (+ x 2)))

(define add2 #f)(define add2 #f)

(set! add2(set! add2 (lambda (x) (+ x (lambda (x) (+ x 2))) 2)))

• The define special The define special form comes in two form comes in two varietiesvarieties

• The three The three expressions to the expressions to the right are entirely right are entirely equivalentequivalent

• The first define The first define form is just more form is just more familiar and familiar and convenient when convenient when defining a functiondefining a function

Page 9: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Mapping functionsMapping functions Common Lisp and Scheme provides several mapping Common Lisp and Scheme provides several mapping

functions.functions. map map (mapcar in Lisp) (mapcar in Lisp) is the most frequently used.is the most frequently used. It takes a function and one or more lists, and returns It takes a function and one or more lists, and returns

the result of applying the function to elements taken the result of applying the function to elements taken from each list, until one of the lists runs out:from each list, until one of the lists runs out:

> (map abs '(3 -4 2 -5 -6))> (map abs '(3 -4 2 -5 -6))(3 4 2 5 6) (3 4 2 5 6) > (map cons '(a b c) '(1 2 3)) > (map cons '(a b c) '(1 2 3)) ((a . 1) (b . 2) (c . 3))((a . 1) (b . 2) (c . 3)) > (map (lambda (x) (+ x 10)) ‘(1 2 3))> (map (lambda (x) (+ x 10)) ‘(1 2 3))(11 12 13)(11 12 13)> (map list ‘(a b c) ‘(1 2 3 4))> (map list ‘(a b c) ‘(1 2 3 4))map: all lists must have same size; arguments map: all lists must have same size; arguments

were: #<procedure:list> (1 2) (a b c)were: #<procedure:list> (1 2) (a b c)

Page 10: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Define Lisp’s Every and Define Lisp’s Every and SomeSome

everyevery and and somesome take a predicate and one or take a predicate and one or more sequencesmore sequences

When given just one sequence, they test When given just one sequence, they test whether the elements satisfy the predicate:whether the elements satisfy the predicate: (every odd? ‘(1 3 5))(every odd? ‘(1 3 5)) #t#t (some even? ‘(1 2 3))(some even? ‘(1 2 3)) #t#t

If given >1 sequences, the predicate must If given >1 sequences, the predicate must take as many args as there are sequences take as many args as there are sequences and args are drawn one at a time from and args are drawn one at a time from them:them: (every > ‘(1 3 5) ‘(0 2 4))(every > ‘(1 3 5) ‘(0 2 4)) #t#t

Page 11: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

everyevery

(define (every f list)(define (every f list)

;; note the use of and;; note the use of and

(if (null? list)(if (null? list)

#t#t

(and (f (first list))(and (f (first list))

(every f (rest list)))))(every f (rest list)))))

Page 12: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

somesome

(define (some f list)(define (some f list)

(if (null? list)(if (null? list)

#f#f

(or (f (first list))(or (f (first list))

(some f (rest list)))))(some f (rest list)))))

Page 13: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Will this work?Will this work?

(define (some f list)(define (some f list)

(not (every (lambda (x) (not (f x)))(not (every (lambda (x) (not (f x)))

list)))list)))

Page 14: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

filterfilter

(filter <f> <list>) returns a list of the (filter <f> <list>) returns a list of the elements of <list> for which satisfy the elements of <list> for which satisfy the predicate <f>predicate <f>

> (filter odd? ‘(0 1 2 3 4 5))> (filter odd? ‘(0 1 2 3 4 5))

(1 3 5)(1 3 5)

> (filter (lambda (x) (> x 98.6))> (filter (lambda (x) (> x 98.6))

‘‘(101.1 98.6 98.1 99.4 102.2))(101.1 98.6 98.1 99.4 102.2))

(101.1 99.4 102.2)(101.1 99.4 102.2)

Page 15: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Example: filterExample: filter(define (myfilter list function)(define (myfilter list function) ;; returns a list of elements of list for which function ;; returns a list of elements of list for which function

is trueis true

(cond ((null? list) nil) (cond ((null? list) nil) ((function (first list))((function (first list)) (cons (first list) (cons (first list) (myfilter (rest list) function)))(myfilter (rest list) function))) (#t (myfilter (rest list) function))))(#t (myfilter (rest list) function))))> (filter ‘(1 2 3 4 5 6 7) even?)> (filter ‘(1 2 3 4 5 6 7) even?)(2 4 6)(2 4 6)

Page 16: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Example: filterExample: filterSuppose we define INTEGERS as a function that Suppose we define INTEGERS as a function that

can generate a list of integers between a min can generate a list of integers between a min and maxand max

(define (integers min max)(define (integers min max) (if (> min max) (if (> min max) ‘ ‘( ) ( ) (cons min (integers (add1 min) max))))(cons min (integers (add1 min) max))))

And PRIME? as a predicate that is true of prime And PRIME? as a predicate that is true of prime numbers and false otherwisenumbers and false otherwise

> (filter prime? (integers 2 20) )> (filter prime? (integers 2 20) )(2 3 5 7 11 13 17 19)(2 3 5 7 11 13 17 19)

Page 17: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Here’s another patternHere’s another pattern

We often want to do something like sum We often want to do something like sum the elements of a sequencethe elements of a sequence(define (sum-list l)(define (sum-list l)

(if (null? l)(if (null? l)

00

(+ (first l) (sum-list (rest l)))))(+ (first l) (sum-list (rest l)))))

And other times we want their productAnd other times we want their product(define (multiply-list l)(define (multiply-list l)

(if (null? l)(if (null? l)

11

(* (first l) (multiply-list (rest l)))))(* (first l) (multiply-list (rest l)))))

Page 18: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Here’s another patternHere’s another pattern

We often want to do something like sum We often want to do something like sum the elements of a sequencethe elements of a sequence(define ((define (sumsum-list l)-list l)

(if (null? l)(if (null? l)

00

((++ (first l) (sum-list (rest l))))) (first l) (sum-list (rest l)))))

And other times we want their productAnd other times we want their product(define ((define (multiplymultiply-list l)-list l)

(if (null? l)(if (null? l)

11

((** (first l) (multiply-list (rest l))))) (first l) (multiply-list (rest l)))))

Page 19: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Example: reduceExample: reduce

Reduce takes (i) a function, (ii) a final Reduce takes (i) a function, (ii) a final value, and (iii) a listvalue, and (iii) a list

Reduce(+ 0 (v1 v2 v3 … vn)) is justReduce(+ 0 (v1 v2 v3 … vn)) is just

V1 + V2 + V3 + … Vn +0V1 + V2 + V3 + … Vn +0 In Lisp notation:In Lisp notation:

> (reduce + 0 ‘(1 2 3 4 5))> (reduce + 0 ‘(1 2 3 4 5))

1515

(reduce * 1 ‘(1 2 3 4 5))(reduce * 1 ‘(1 2 3 4 5))

120120

Page 20: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Example: reduceExample: reduce

(define (reduce function final list)(define (reduce function final list)

(if (null? list) (if (null? list)

finalfinal

(function (function

(first list)(first list)

(reduce function final (rest (reduce function final (rest list)))))list)))))

Page 21: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Using reduceUsing reduce(define (sum-list list) (define (sum-list list)

;; returns the sum of the list elements;; returns the sum of the list elements

(reduce + 0 list))(reduce + 0 list))

(define (mul-list list) (define (mul-list list)

;; returns the sum of the list elements;; returns the sum of the list elements

(reduce * 1 list))(reduce * 1 list))

(define (copy-list list) (define (copy-list list)

;; copies the top level of a list;; copies the top level of a list

(reduce cons ‘() list))(reduce cons ‘() list))

(define (append-list list)(define (append-list list)

;; appends all of the sublists in a list;; appends all of the sublists in a list

(reduce append ‘() list))(reduce append ‘() list))

Page 22: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Page 23: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

ComposingComposingfunctionsfunctions

> compose> compose

#<procedure:compose>#<procedure:compose>

> (define (square x) (* x x))> (define (square x) (* x x))

> (define (double x) (* x 2))> (define (double x) (* x 2))

> (square (double 10))> (square (double 10))

400400

> (double (square 10))> (double (square 10))

200200

> (define sd (compose square double))> (define sd (compose square double))

> (sd 10)> (sd 10)

400400

> ((compose double square) 10)> ((compose double square) 10)

200200

Page 24: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Here’s how to define itHere’s how to define it

(define (my-compose f1 f2)(define (my-compose f1 f2)

(lambda (x) (f1 (f2 x))))(lambda (x) (f1 (f2 x))))

Page 25: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Variables, free and boundVariables, free and bound

In this function, to what does the In this function, to what does the variable GOOGOL refer?variable GOOGOL refer?

(define (big-number? x)(define (big-number? x)

; returns true if x is a really big ; returns true if x is a really big numbernumber

(> x GOOGOL))(> x GOOGOL))The The scopescope of the variable X is just of the variable X is just the body of the function for which it’s the body of the function for which it’s a parameter.a parameter.

Page 26: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Here, GOOGOL is a global Here, GOOGOL is a global variablevariable

> (define GOOGOL (expt 10 100))> (define GOOGOL (expt 10 100))

> GOOGOL> GOOGOL

1000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

> (define (big-number? x) (> x GOOGOL))> (define (big-number? x) (> x GOOGOL))

> (big-number? (add1 (expt 10 100)))> (big-number? (add1 (expt 10 100)))

#t #t

Page 27: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Which X is accessed at the Which X is accessed at the end?end?

> (define GOOGOL (expt 10 100))> (define GOOGOL (expt 10 100))

> GOOGOL> GOOGOL

1000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

> (define x -1)> (define x -1)

> (define (big-number? x) (> x GOOGOL))> (define (big-number? x) (> x GOOGOL))

> (big-number? (add1 (expt 10 100)))> (big-number? (add1 (expt 10 100)))

#t #t

Page 28: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Variables, free and boundVariables, free and bound

In the body of this function, we say In the body of this function, we say that the variable (or symbol) X is that the variable (or symbol) X is boundbound and GOOGOL is and GOOGOL is freefree. .

(define (big-number? x)(define (big-number? x)

; returns true if X is a really big ; returns true if X is a really big numbernumber

(> X GOOGOL))(> X GOOGOL)) If it has a value, it has to be bound If it has a value, it has to be bound somewhere elsesomewhere else

Page 29: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

let creates local variableslet creates local variables

> (let [(pi 3.1415)> (let [(pi 3.1415)

(e 2.7168)](e 2.7168)]

(big-number? (expt pi e)))(big-number? (expt pi e)))

#f#f The general form is (let <varlist> . The general form is (let <varlist> .

<body>)<body>) It creates a local environment, binding the It creates a local environment, binding the

variables to their initial values, and variables to their initial values, and evaluates the expressions in <body>evaluates the expressions in <body>

Page 30: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Let’s just syntactic sugar for Let’s just syntactic sugar for lambdalambda

((lambda (pi e) (big-number? (expt pi e)))((lambda (pi e) (big-number? (expt pi e)))

3.14153.1415

2.7168)2.7168)and this is how we did it back before and this is how we did it back before ~1973~1973

What happens here:What happens here:

(let [ (x 10) (xx (* x 2)) ](let [ (x 10) (xx (* x 2)) ]

(printf “x: ~s, xx: ~s\n” x xx))(printf “x: ~s, xx: ~s\n” x xx))

Page 31: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

let and let*let and let*

The let special form evaluates all of the The let special form evaluates all of the expressions that provide the initial values, expressions that provide the initial values, and then creates a new environment in and then creates a new environment in which the local variables are bound to which the local variables are bound to themthem

let* expands to a series of nested letslet* expands to a series of nested lets (let* [(x 100)(xx (* 2 x))] (foo x xx) )(let* [(x 100)(xx (* 2 x))] (foo x xx) ) (let [(x 100)] (let [(x 100)]

(let [(xx (* 2 x))] (let [(xx (* 2 x))] (foo x xx) ) ) (foo x xx) ) )

Page 32: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

What happens here?What happens here?

> (define X 10)> (define X 10)

> (let [(X (* X X))]> (let [(X (* X X))] (printf “X is ~s\n” X) (printf “X is ~s\n” X) (set! X 1000) (set! X 1000) (printf “X is ~s\n” X) (printf “X is ~s\n” X) -1 ) -1 )

??????

> X> X

??? ???

Page 33: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

What happens here?What happens here?

> (define X 10)> (define X 10) (let [(X (* X X))](let [(X (* X X))]

(printf “X is ~s\n” X) (printf “X is ~s\n” X) (set! X 1000) (set! X 1000) (printf “X is ~s\n” X) (printf “X is ~s\n” X) -1 ) -1 )

X is 100X is 100

X is 1000X is 1000

-1-1

> X> X

10 10

Page 34: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

What happens here?What happens here?

> (define GOOGOL (expt 10 100))> (define GOOGOL (expt 10 100))

> (define (big-number? x) (> x > (define (big-number? x) (> x GOOGOL))GOOGOL))

> (let [(GOOGOL (expt 10 101))]> (let [(GOOGOL (expt 10 101))]

(big-number? (add1 (expt 10 (big-number? (add1 (expt 10 100))))100))))

??????

Page 35: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

What happens here?What happens here?

> (define GOOGOL (expt 10 100))> (define GOOGOL (expt 10 100))

> (define (big-number? x) (> x GOOGOL))> (define (big-number? x) (> x GOOGOL))

> (let [(GOOGOL (expt 10 101))]> (let [(GOOGOL (expt 10 101))]

(big-number? (add1 (expt 10 100))))(big-number? (add1 (expt 10 100))))

#t#tThe free variable GOOGOL is looked up The free variable GOOGOL is looked up in the environment in which the big-in the environment in which the big-number? Function was defined!number? Function was defined!

Page 36: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Dynamic vs. Static ScopingDynamic vs. Static Scoping

Programming languages either use Programming languages either use dynamic or static (aka lexical) scopingdynamic or static (aka lexical) scoping

In a statically scoped language, free In a statically scoped language, free variables in functions are looked up in variables in functions are looked up in the environment in which the function is the environment in which the function is defineddefined

In a dynamically scoped language, free In a dynamically scoped language, free variables are looked up in the variables are looked up in the environment in which the function is environment in which the function is called called

Page 37: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

ClosuresClosures

Lisp is a Lisp is a lexically scoped lexically scoped language.language.Free variables referenced in a function Free variables referenced in a function those are looked up in the environment those are looked up in the environment in which the function is defined.in which the function is defined.Free variables are those a function (or block) Free variables are those a function (or block) doesn’t create scope for.doesn’t create scope for.

A A closureclosure is a function that remembers is a function that remembers the environment in which it was the environment in which it was createdcreated

An An environmentenvironment is just a collection of is just a collection of variable bindings and their values.variable bindings and their values.

Page 38: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Closure exampleClosure example> (define (make-counter)> (define (make-counter) (let ((count 0)) (lambda () (set! count (add1 (let ((count 0)) (lambda () (set! count (add1

count)))))count)))))> (define c1 (make-counter))> (define c1 (make-counter))> (define c2 (make-counter))> (define c2 (make-counter))> (c1)> (c1)11> (c1)> (c1)22> (c1)> (c1)33> (c2)> (c2)??????

Page 39: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

A fancier make-counterA fancier make-counter

Write a fancier make-counter function Write a fancier make-counter function that takes an optional argument that that takes an optional argument that specifies the incrementspecifies the increment> (define by1 (make-counter))> (define by1 (make-counter))

> (define by2 (make-counter 2))> (define by2 (make-counter 2))

> (define decrement (make-counter -1))> (define decrement (make-counter -1))

> (by2)> (by2)

22

(by2)(by2)

4 4

Page 40: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Optional arguments in Optional arguments in SchemeScheme

(define (make-counter . args)(define (make-counter . args) ;; args is bound to a list of the actual arguments ;; args is bound to a list of the actual arguments

passed to the functionpassed to the function

(let [(count 0)(let [(count 0)

(inc (if (null? args) 1 (first args)))](inc (if (null? args) 1 (first args)))]

(lambda ( ) (set! count (+ count (lambda ( ) (set! count (+ count inc))))) inc)))))

Page 41: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Keyword arguments in Keyword arguments in SchemeScheme

Scheme, like Lisp, also has a way to define Scheme, like Lisp, also has a way to define functions that take functions that take keyword argumentskeyword arguments (make-counter)(make-counter) (make-counter :initial 100)(make-counter :initial 100) (make-counter :increment -1)(make-counter :increment -1) (make-counter :initial 10 :increment -2)(make-counter :initial 10 :increment -2)

Different Scheme dialects have introduced Different Scheme dialects have introduced different ways to mix positional arguments, different ways to mix positional arguments, optional arguments, default values, optional arguments, default values, keyword argument, etc.keyword argument, etc.

Page 42: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,

Closure tricksClosure tricks

We can write several We can write several functions that are functions that are closed in the same closed in the same environment, which can environment, which can then provide a private then provide a private communication channelcommunication channel

(define foo #f)(define foo #f)

(define bar #f)(define bar #f)

(let ((secret-msg "none"))(let ((secret-msg "none"))

(set! foo(set! foo (lambda (msg) (lambda (msg) (set! secret-msg msg))) (set! secret-msg msg)))

(set! bar(set! bar (lambda () secret-msg))) (lambda () secret-msg)))

(display (bar)) ; prints "none"(display (bar)) ; prints "none"

(newline)(newline)

(foo ”attack at dawn")(foo ”attack at dawn")

(display (bar)) ; prints ”attack (display (bar)) ; prints ”attack at dawn"at dawn"

Page 43: Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,