Programming Languages (CSC-302 99S)


Class 05: Untyped Functional Programming in Scheme

Back to Introduction to Functional Programming. On to Laboratory Session: Scheme.

Held Wednesday, February 3

Summary

Contents


An introduction to LISP-like languages

Lists in LISP-like languages

(How's that for alliteration?)

An introduction to Scheme

Scheme in the MathLAN

Some sample scheme stuff

;;; Compute the factorial of n.
(define (fact n)
  (if (= n 0) 1 (* n (fact (- n 1)))))

;;; Add x to the end of list l.
(define (addend x l)
  (if (null? l)
    (list x)
    (cons (car l) (addend x (cdr l)))))

;;; Reverse the list l.
;;; Yes, this function is a standard Scheme function, but it is
;;;   helpful to consider how we might write it.
(define (reverse l)
  (if (null? l)
    ()
    (addend (car l) (reverse (cdr l)))))

;;; Here is an attempt to write a more efficient reverse function.

;;; Create the list of the elements of l in reverse order followed
;;; by the elements of r.  For example, (tailrev '(a b c) '(3 2 1))
;;; will create the list (c b a 3 2 1).
(define (tailrev l r)
  (if (null? l)
    r
    (tailrev (cdr l) (cons (car l) r))))
;;; Reverse the list l, using our new helper function.
(define (reverse l)
  (tailrev l ()))

Short Lab

We experimented with the functions given above.


History


Back to Introduction to Functional Programming. On to Laboratory Session: Scheme.


Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS302/99S/Outlines/outline.05.html

Source text last modified Wed Feb 3 15:02:13 1999.

This page generated on Wed Feb 3 15:04:15 1999 by SiteWeaver. Validate this page.

Contact our webmaster at rebelsky@math.grin.edu