Docsity

List Processing Language - Programming Languages - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download List Processing Language - Programming Languages - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Programming

Languages

LISP

LISt Processing Language

A Brief Introduction

  • Atoms :
    • numbers : (real 1.0, integer 1)
    • symbols : a consecutive sequence of characters (no space) e.g., a, x, price-of-beef.
    • two special symbols: T and NIL for logical true and false.
    • strings : a sequence of characters bounded by double quotes e.g., "this is red".

Valid objects (S-expression)

  • Lists : a list of atoms and/or lists, bounded by ( and ) e.g., (a b c), (a (b c))
  • top elements of a list – first level
  • example:
  • top elements of list (a b c) are a, b, and c
  • top elements of list (a (b c)) are a and (b c)
  • nil : empty list, same as ().

Valid objects (S-expression)

  • Function calls
    • Examples:

(sqrt 4) 2

  • Evaluation of S-expression
  1. Evaluate an atom.
  • numerical and string atoms evaluate to themselves;
  • symbols evaluate to their values if they are assigned values, return Error, otherwise;
  • the values of T and NIL are themselves.
  • Evaluation of S-expression
    • Examples

(sqrt x) Error: The variable X is unbound.

(+ (sqrt 4) 4.0)

  1. To assign a value to a symbol ( setq, set, setf )
  • setq is a special form of function (with two arguments);
  • the first argument is a symbol which will not be evaluated;
  • the second argument is a S-expression, which will be evaluated;
  • the value of the second argument is assigned to be the value of the first argument

(setq x 3.0)

x

(setq y x)

; the value of x is assigned as the value of y

y

(+ x y)

Functions:

  • Math functions:
    • +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min
  • Constructors: Cons, list and append
    • ( cons 'x L) ; insert symbol x at the front of list L, which is (X A B C) ; (A B C) - >( list 'a 'b 'c) ; making a list with the arguments as its elements (A B C) ; if a, b, c have values A, B, C, then (list a b c) ; returns list (A B C) - >( append '(a b) '(c d)) (A B C D) ; appends one list in front of another

Functions: (cont’)

  • Selectors: nth, first, rest, car, cdr…

>(first '(a s d f))

a

>(first '((a s) d f))

(a s)

>(rest '(a s d f))

(s d f).

>(rest '((a s) d f))

(d f)

>(rest '((a s) (d f))

((d f))

Functions (cont’)

  • Other operations:

>( reverse L) ; reverses a list

(C B A)

>( length L) ; returns the length of list L

Predicates

  • a special function which returns NIL if the

predicate is false, T or anything other than NIL, otherwise

  • = , > , < , >= , <= for numerical values;
  • equal eq for others
  • atom : test if x is an atom
  • listp : test if x is a list
  • Also number , symbolp , null

Defining LISP functions

  • ( defun func-name (arg-1 ... Arg-n) func-body)
    • Example:

(defun y-plus (x) (+ x y)) ;definition of y-plus

(setq y 2) (y-plus 23) 25

  • Local and global variables: local variables defined in

function body

  • Function returns the value of last expression in its body
  • Conditional control: if, when and cond
    • if

(if )

> (setq SCORE 78)

> (if (> score 85) ‘HIGH

(if (and (< score 84) (> score 65)) ‘MEDIUM ‘LOW))

> MEDIUM