Comparative Programming Languages - PowerPoint PPT Presentation

About This Presentation
Title:

Comparative Programming Languages

Description:

The basic process of computation is fundamentally different in a FPL than in an ... It was developed by Guy L. Steele and Gerald Jay Sussman at MIT ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 21
Provided by: tjh5
Category:

less

Transcript and Presenter's Notes

Title: Comparative Programming Languages


1
Comparative Programming Languages
  • Functional programming with Lisp/Scheme

2
Fundamentals of Functional Programming
Languages
  • The objective of the design of a functional
    programming language (FPL) is to mimic
    mathematical functions to the greatest extent
    possible
  • The basic process of computation is fundamentally
    different in a FPL than in an imperative language
  • In an imperative language, operations are done
    and the results are stored in variables for later
    use
  • Management of variables is a constant concern and
    source of complexity for imperative programming
  • In an FPL, variables are not necessary, as is the
    case in mathematics

CS 363 Spring 2005 GMU
2
3
Lisp
  • Lisp based on lambda calculus (Church)
  • Uniform representation of programs and data using
    single general data structure (list)
  • Interpreter based (written in Lisp)
  • Automatic memory management
  • Evolved over the years
  • Dialects COMMON LISP, Scheme

CS 363 Spring 2005 GMU
3
4
Scheme
  • Scheme is a functional programming language and a
    dialect of Lisp. It was developed by Guy L.
    Steele and Gerald Jay Sussman at MIT
  • Use Programming Languages -gtDr. Scheme here in
    the Comp Sys lab

CS 363 Spring 2005 GMU
4
5
Common Lisp
  • Use clisp for Common Lisp here in the systems lab

CS 363 Spring 2005 GMU
5
6
Scheme (dr scheme, guile)
  • (define (gcd u v)
  • (if ( v 0)
  • u
  • (gcd v (remainder u v))
  • )
  • )
  • (define (reverse l)
  • (if (null? l) l
  • (append (reverse (cdr l))(list (car l)))
  • )
  • )

CS 363 Spring 2005 GMU
6
7
Scheme (dr scheme, guile)
  • Using guile (gnu scheme)
  • (load "slides.scm")
  • (gcd 56 108) --gt 4
  • (reverse '(2 3 556)) --gt (556 3 2)

CS 363 Spring 2005 GMU
7
8
Common Lisp (clisp)
  • (defun mygcd (u v)
  • (if ( v 0)
  • u
  • (mygcd v (rem u v))
  • )
  • )
  • (defun myreverse (l)
  • (if (null l) l
  • (append (myreverse (cdr l))(list (car l)))
  • )
  • )
  • (load "slides.lsp"), (mygcd 56 108) --gt 4
  • (myreverse '(2 3 556)) --gt (556 3 2)

8
9
Scheme
  • (define (gcd u v)
  • (if ( v 0)
  • u
  • (gcd v (remainder u v))
  • )
  • )
  • Once defined in the interpreter
  • (gcd 25 10)
  • 5

9
10
Scheme/Lisp
  • expression ? atom list
  • atom ? number string
  • identifier character boolean
  • list ? ( expression-sequence )
  • expression-sequence ? expression
  • expression-sequence expression

10
11
Scheme Expression vs. C
  • In Scheme
  • ( 3 ( 4 5 ))
  • (and ( a b)(not ( a 0)))
  • (gcd 10 35)
  • In C
  • 3 4 5
  • (a b) (a ! 0)
  • gcd(10,35)

11
12
Evaluation Rules for Scheme Expressions
  1. Constant atoms (numbers, strings) evaluate to
    themselves
  2. Identifiers are looked up in the current
    environment and replaced by the value found there
    (using dynamically maintained symbol table)
  3. A list is evaluated by recursively evaluating
    each element in the list as an expression the
    first expression must evaluate to a function.
    The function is applied to the evaluated values
    of the rest of the list.

12
13
Scheme Evaluation
  • To evaluate ( ( 2 3)( 4 5))
  • is the function must evaluate the two
    expressions ( 2 3) and ( 4 5)
  • To evaluate ( 2 3)
  • is the function must evaluation the two
    expressions 2 and 3
  • 2 evaluates to the integer 2
  • 3 evaluates to the integer 3
  • 2 3 5
  • To evaluate ( 4 5) follow similar steps
  • 5 9 45



4
5
2
3
13
14
Scheme Conditionals
  • If statement
  • (if ( v 0)
  • u
  • (gcd v (remainder u v))
  • )
  • (if ( a 0) 0 (/ 1 a))
  • Cond statement
  • (cond (( a 0) 0)
  • (( a 1) 1)
  • (else (/ 1 a))
  • )

14
15
Example of COND (Scheme)
  • (DEFINE (compare x y)
  • (COND
  • ((gt x y) (DISPLAY x is greater than y))
  • ((lt x y) (DISPLAY y is greater than x))
  • (ELSE (DISPLAY x and y are equal))
  • )
  • )

15
16
Example of COND (Lisp)
  • (defun compare (x y)
  • (COND
  • ((gt x y)(format t x is greater than y))
  • ((lt x y)(format t y is greater than x))
  • (t (format t x and y are equal))
  • )
  • )

16
17
Predicate Functions (Scheme)
  • EQ? takes two symbolic parameters it returns T
    if both parameters are atoms and the two are the
    same
  • e.g., (EQ? 'A 'A) yields T
  • (EQ? 'A '(A B)) yields ()
  • Note that if EQ? is called with list parameters,
    the result is not reliable
  • EQ? does not work for numeric atoms (use )

17
18
Predicate Functions (Lisp)
  • EQ takes two symbolic parameters it returns T
    if both parameters are atoms and the two are the
    same
  • e.g., (eq 'A 'A) yields T
  • (eq 'A '(A B)) yields nil

18
19
Predicate Functions (Scheme)
  • LIST? takes one parameter it returns T if the
    parameter is a list otherwise()
  • 3. NULL? takes one parameter it returns T if
    the parameter is the empty list otherwise()
  • Note that NULL? returns T if the
    parameter is()
  • 4. Numeric Predicate Functions
  • , ltgt, gt, lt, gt, lt, EVEN?, ODD?, ZERO?,
    NEGATIVE?

19
20
Predicate Functions (Lisp)
  • LISTP takes one parameter it returns T if the
    parameter is a list otherwise nil
  • 3. NULL takes one parameter it returns T if the
    parameter is the empty list otherwise nil
  • Note that NULL returns T if the
    parameter is()
  • 4. Numeric Predicate Functions
  • , ltgt, gt, lt, gt, lt, EVENP, ODDP, ZEROP

20
Write a Comment
User Comments (0)
About PowerShow.com