Introduction to Functional Programming and Scheme

 

 

 

Note: Some of the following material comes from Ben-Ari Chapter 16 on Functional Programming. However, that chapter focuses on the ML language, and I wish to focus on Scheme. Most of the material on this page comes from the Dybvig text on ANSI Scheme, Chapter 1.

 

 

Introduction

 

 

  1. Functional languages support a conceptually pure view of computing.
    - Church's lambda calculus computation model
    - Based on mathematical functions, not von Neumann architecture
    - No side effects (assignment)
    - No memory smearing (stray pointers)
    - Functions are first-class values (usually)
    - Simple language structure (grammar -- see Dybvig p. 50)
  2.  

  3. Lisp is the original
    - McCarthy at MIT
    - Late 1950s
    - Legend of the 24-hour interpreter
    - Many dialects have been developed
  4.  

  5. Scheme is a popular dialect of Lisp
    - Sussman and Steele at MIT
    - Mid 1970s
    - Originally interpreted
    - Sophisticated compilers exist
    - Normally implemented as interactive programming environment
    ("read-evaluate-print" cycle)
  6.  

  7. Installing Scheme
    - \\eccentric\pubware\repository\DrScheme50
    - read.me file has instructions for copying to diskette
    - DrScheme is Windows programming environment (big)
    - MzScheme is Dos programming environment (small)
    - install.bat will launch installer program for both

 

 

 

 

Some language characteristics

 

  1. Operators for numbers, characters, strings, lists, vectors
  2. Objects (data values) dynamically allocated on heap (automatically)
  3. Objects dynamically deallocated from heap (garbage collection)
  4. Has variables but need not have assignment!
  5. Variables are bound to objects at runtime (e.g. used as formal parameters)
  6. Program and data have same representation (symbols and lists)
  7. Programs are block-structured
  8. Identifiers have lexical (static) scope within block
  9. Functions are called procedures
  10. Procedures are first class objects (see Chapter 7 notes)
    - Bound to variables
    - Passed as parameters
    - Returned from procedures
  11. Recursion is used for iteration
    - Tail recursion
    - Optimization
  12. Supports continuations
    - procedure that represents program execution state snapshot.
    - created at runtime
    - can be invoked later (resume)
    - is first class object
    - enables coroutines and multithreading

 

 

 

 

Scheme Lexemes

 

  1. small number of keywords (define, if, lambda, quote, let, #t, #f . . .)
  2. identifiers formed from alphanumerics and special symbols
    - compiler must be able to distinguish it from a value
    - not restricted to alpha in first character, though
    - no length restriction
    - compiler is not case sensitive
  3. data constants: numbers, characters, strings, quoted lists, etc
  4. comment set off by ‘;’ and extends to end of line

 

 

Scheme Syntax

 

  1. formal syntax defined Dybvig, pp 233-236.
  2. expression: enclosed in parentheses, items separated by white space
  3. lists are expressions 
  4. vector: list used for vector/matrix operations (put # in front of list)

 

 

Some Procedure Naming Conventions

 

  1. those that return predicates (#t or #f), names end in ?
  2. those that work with char, string, vector, named type-name (type is char,etc)
  3. those that convert objects, named pre->post (fill in type names)
  4. those that produce side effects end in ‘!’