Special Forms and Higher Order Procedures: Understanding and in Lisp, Slides of Computer Programming

An in-depth explanation of special forms and higher order procedures in lisp. It covers the concepts of and, or, and short-circuiting, as well as the idea of capturing patterns and using them to create procedures. The document also discusses type analysis and the use of types to understand code.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

167 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 6: HOPs, Types, Nimrod
and and or are not procedures, but special forms. They have a common feature of “short-
circuiting” at a value if their task is completed early, and then they do not need to
evaluate all of their arguments.
Scheme
1. Special Forms
(a) and – (and arg1 arg2 …) Evaluates arguments from left to right, stopping
at the first one that evaluates to false and returning false. Should all arguments
evaluate “true-ishly”, returns the values of the last argument.
Example:
(and (not null? lst)
(= (car lst) 3))
If the list lst is not null, then the procedure continues to check if the first
element of the list is a three. If it is, the return is true; if not the return is false.
However, if the list lst is null, the entire thing has a return of false even though
the second argument was not evaluated.
(b) or – (or arg1 arg2 …) Evaluates arguments from left to right, stopping at
the first one that evaluates to “true-ish” and returns that value. Should all the
arguments evaluate to false, returns false.
Example:
(or (= (car lst) 3)
(check-3 (cdr lst)))
If the first element of the list is three, procedure returns true. If not, it
continues to the next, and performs the same check. If true, it returns true; if
false, it returns false.
As an aside and reminder, a procedure takes in inputs and returns outputs.
Higher Order Procedures
The idea of programming is to capture patterns, as in the summation of n, and n – 1, or
the squares of n, and n-1, and use those patterns to form procedures for evaluation.
(The following are de-sugared to show the multiple lambdas.)
Docsity.com
pf3

Partial preview of the text

Download Special Forms and Higher Order Procedures: Understanding and in Lisp and more Slides Computer Programming in PDF only on Docsity!

Lecture 6: HOPs, Types, Nimrod

and and or are not procedures, but special forms. They have a common feature of “short- circuiting” at a value if their task is completed early, and then they do not need to evaluate all of their arguments.

Scheme

  1. Special Forms

(a) and – (and arg1 arg2 …) Evaluates arguments from left to right, stopping at the first one that evaluates to false and returning false. Should all arguments evaluate “true-ishly”, returns the values of the last argument.

Example: (and (not null? lst) (= (car lst) 3))

If the list lst is not null, then the procedure continues to check if the first element of the list is a three. If it is, the return is true; if not the return is false. However, if the list lst is null, the entire thing has a return of false even though the second argument was not evaluated.

(b) or – (or arg1 arg2 …) Evaluates arguments from left to right, stopping at the first one that evaluates to “true-ish” and returns that value. Should all the arguments evaluate to false, returns false.

Example: (or (= (car lst) 3) (check-3 (cdr lst)))

If the first element of the list is three, procedure returns true. If not, it continues to the next, and performs the same check. If true, it returns true; if false, it returns false.

As an aside and reminder, a procedure takes in inputs and returns outputs.

Higher Order Procedures

The idea of programming is to capture patterns, as in the summation of n, and n – 1, or the squares of n, and n-1, and use those patterns to form procedures for evaluation.

(The following are de-sugared to show the multiple lambdas.)

(define sum (lambda (f x y dx) (if (> x y) 0 (+ (f x) (sum f (+ x dx) y dx)))) Å the argument f in this case represents a function, which we know because it is listed in the position of an operation (define make-adder (lambda (amt) (lambda (x) (+ x amt))))

(define add-3 (make-adder 3))

(add-3 5) ;Value: 8

((make-adder 3) 5)

(((lambda (amt) (lambda (x) (+ x amt))) 3) 5) ((lambda (x) (+ x 3)) 5) add- (+ 5 3) Æ 8

We want to compose two functions, f and g.

(define compose (lambda (f g) (lambda (x) (f (g x))))) f represents the square function (define square (lambda (x) (* x x))) g represents the increment function (define inc (lambda (x) (+ x 1)))

Type Analysis

(define inc-square (compose square inc)) (inc-square 3) Æ 16

The compose function takes in two procedures and returns a procedure. These are the “types” involved. For this class, it was notated the following way:

num: number