CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

Within the actual body of the code, we merely applied the functionality passed ... If you spontaneously generate a lambda-body to pass to another function, you ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 22
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Module 21
  • Lambda Bodies

2
In the Last Module
We started with idea of going from this
(contains-number? ( 1 2 3 4 5) 6 )
A function which takes in a list of numbers and a
number.
3
In the Last Module
To this
(contains? (1 2 3 4 5) 6)
A function which takes in a list of numbers, a
number, and some sort of functionality that acts
on our numbers within the body of the code.
4
In our code
(define (contains? in-test in-list value)
(cond ((empty? in-list) false)
(else (cond ((in-test (first in-list) value)
true) (else (contains? in-test

(rest in-list)
value ))))))
Within the actual body of the code, we merely
applied the functionality passed in as an
argument to the first of our list and the value
passed in. This was made possible by exploiting
Schemes simple set of evaluation rules.
5
This has implications
The ability to pass functionality as an argument
to another function has certain implications and
raises certain questions. 1) Just what does
(define (f x) ) really mean? 2) If we can
separate a functions actual
functionality from its name, is there a way just
pass function bodies?
6
Lambda bodies
Besides being an oh-so recognizable symbol for
DrScheme, the image shown to the left (minus the
nifty coloring) is the greek symbol ? (lambda)
7
Lambda
A lambda expression is nothing more than a
function body without a name.
8
?
Lambda, in Scheme, represents a nameless function
body.
(lambda (x y) ( ( x 3) ( - y 2)))
This is a nameless function. It takes in two
arguments (x y), and performs a calculation on
those two arguments (x 3) (y 2).
9
? the syntax
Lambda has a very simple syntax
(lambda (parameter1 parameter2 parameter-n)
ltexpression to evaluategt)
(lambda (in-num) ( in-num 1))
(lambda (in-sym) (cond ((symbol? in-sym
a) true)
(else false)))
10
?
All semester long, weve seen that define (or
some derivative of it) will bind a name to a
value. For example
Binding a name to a value
(define my-value (1 3 4 5))
Binding a name to a structure definition
(define-struct my-struct (a b c))
Binding a name to a function definition
(define (my-function x) ( x 1))
11
? Our original questions
Back to our original questions
1) Just what does (define (f x) ltexpressiongt)
really mean?
(define (f x) ltexpressiongt) creates a function f
with a parameter x that evaluates ltexpressiongt.
But behind the scenes, we have the following
(define f (lambda (x) ltexpressiongt))
12
Lambda
So if we combine the idea that define binds a
name to a value (including a function body) and
the idea that lambda produces nameless function
bodies, we see that (define (square x) ( x
x)) Is really just shorthand for this (define
square (lambda (x) ( x x)))
13
Example
(define factorial (lambda (num) (if
(zero? num) 1 ( num
(factorial (- num 1))))))
A more traditional example of using no-name
functions.
14
Lambda
So lets consider (define square (lambda (x)
( x x)))
The (lambda) part merely means heres a function
that takes in a parameter, called x, and performs
the following operations. This function,
identified by the lambda expression, is defined
as being the symbol square.
15
Lambda
So lets consider (define square (lambda (x)
( x x)))
In short, the "lambda" tells Scheme to compile
the function body that follows, and return the
executable result. That is, lambda is like a
function that returns a function.
16
Lambda
So lets consider (define square (lambda (x)
( x x)))
The name "lambda" itself comes from something
called "lambda calculus", the mathematical
precursor to functional programming that was
developed by Alonzo Church.
Read more The Advent of the Algorithm, David
Berlinski
17
? More Examples
(define (convertCF in-lon) (cond ((empty?
in-lon) empty) (else (cons ( ( (first
in-lon)1.8) 32) (convertCF (rest
in-lon)))))
(define convertCF (lambda (in-lon)
(cond ((empty? in-lon) empty) (else
(cons ( ( (first in-lon)1.8) 32)
(convertCF (rest in-lon)))))))
THESE ARE EQUIVALENT!
18
? Our original questions
2) If we can separate a functions actual
functionality from its name, is there a way just
pass function bodies?
Yep. If youll think back a bit
Here we are stripping away the functionality of
addition from the name and passing it as an
argument.
19
? Our original questions
Here I pass in a nameless function body to
my-function.
20
? Its not all sun and roses
Before you fall in love with the idea of
lambda-bodies and passing nameless functions,
remember that using functions in this manner has
its draw-backs 1) Less readable code! If you
spontaneously generate a lambda-body to
pass to another function, you include the
entire code for the lambda body in the
code! Clutter! 2) Names are more useful than
you think! Once youve sent a lambda-body
on its way, youve lost your handle on the
function. If you need to use the same
lambda-body in multiple places, youd have
to duplicate the code every time!
21
Questions?
Write a Comment
User Comments (0)
About PowerShow.com