Presentation is loading. Please wait.

Presentation is loading. Please wait.

Modern Programming Languages Lecture 18 Fakhar Lodhi

Similar presentations


Presentation on theme: "Modern Programming Languages Lecture 18 Fakhar Lodhi"— Presentation transcript:

1 Modern Programming Languages Lecture 18 Fakhar Lodhi

2 LISP Invented by John McCarthy (1958)
Two simple data structure (atoms and lists) Heavy use of recursion Interpretive language Variations Scheme Common Lisp (de facto industrial standard) Most widely used AI programming language Functional Programming Paradigm Low maintenance overhead

3 Valid Objects Atoms may be; 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"

4 Valid Objects Lists: A list of atoms and/or lists, bounded by ( and ). For example (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’ stands for empty list, same as ()

5 Function Calls Its also a list Uses prefix notation:
(function-name arg1,arg2 ... argN) It returns function value for the given list of arguments Functions are either provided by LISP function library or defined by the user

6 Function Calls Examples: > (+ 1 3 5) 9 > (/ 3 5) 3/5
> (/ 3.0 5) > (sqrt 4) 2 Command Line Prompt

7 Evaluation of S-expression
1) Evaluating an atom Numerical and string atoms evaluate to themselves Symbols evaluate to their values if they are assigned values, otherwise return Error The values of T and NIL are themselves S-expression From Wikipedia, the free encyclopedia. An S-expression (S stands for symbolic) is a convention for representing data or an expression in a computer program in a text form. S-expressions are used in the Lisp programming language and Lisp-derived languages such as Scheme and DSSSL, and as mark-up in communications protocols like IMAP and John McCarthy's CBCL. The details of the syntax and supported data types vary in the different languages, but the most common feature is the extensive use of prefix notation with explicit use of brackets (affectionately known as Cambridge Polish notation). S-expressions are used for both code and data in Lisp (see McCarthy Recursive Functions of Symbolic Expressions at ). S-expressions were originally intended only as machine representations of human-readable M-expressions, but Lisp programmers soon started using S-expressions as the default notation. S-expressions can either be single objects such as numbers, LISP atoms including the special atoms nil and t, or cons pairs, written as (x . y). Longer lists are made up of nested cons pairs, for example (1 . (2 . (3 . nil))) which can also be written more intelligibly as (1 2 3). Program code can be written in S-expressions, using prefix notation. An extra piece of syntactic sugar for writing Lisp programs is that the common expression (quote x) can be written with the abbreviation 'x. Example in Lisp: (defun factorial (x) (cond ((eq x 1) 1) (t (* x (factorial (- x 1)))))) Example in Scheme: (define (factorial x) (if (= x 1) 1 (* x (factorial (- x 1)))))

8 Evaluation of S-expression
2) Evaluate a list - evaluate every top element of the list as follows, unless explicitly forbidden: The first element is always a function name; evaluating it means to call the function body Each of the rest elements will then be evaluated, and their values returned as the arguments for the function S-expression From Wikipedia, the free encyclopedia. An S-expression (S stands for symbolic) is a convention for representing data or an expression in a computer program in a text form. S-expressions are used in the Lisp programming language and Lisp-derived languages such as Scheme and DSSSL, and as mark-up in communications protocols like IMAP and John McCarthy's CBCL. The details of the syntax and supported data types vary in the different languages, but the most common feature is the extensive use of prefix notation with explicit use of brackets (affectionately known as Cambridge Polish notation). S-expressions are used for both code and data in Lisp (see McCarthy Recursive Functions of Symbolic Expressions at ). S-expressions were originally intended only as machine representations of human-readable M-expressions, but Lisp programmers soon started using S-expressions as the default notation. S-expressions can either be single objects such as numbers, LISP atoms including the special atoms nil and t, or cons pairs, written as (x . y). Longer lists are made up of nested cons pairs, for example (1 . (2 . (3 . nil))) which can also be written more intelligibly as (1 2 3). Program code can be written in S-expressions, using prefix notation. An extra piece of syntactic sugar for writing Lisp programs is that the common expression (quote x) can be written with the abbreviation 'x. Example in Lisp: (defun factorial (x) (cond ((eq x 1) 1) (t (* x (factorial (- x 1)))))) Example in Scheme: (define (factorial x) (if (= x 1) 1 (* x (factorial (- x 1)))))

9 Evaluation of S-expression
Examples : 3/5+4 > (+ (/ 3 5) 4) 23/5 > (+ (sqrt 4) 4.0) 6.0 > (sqrt x) Error: The variable X is unbound

10 Evaluation of S-expression
3) To assign a value to a symbol use: 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) 3.0 > x 3.0

11 > (setq y x) 3.0 > y > (+ x y) 6.0

12 To forbid evaluation of a symbol use: quote or ’
> (quote x) x > 'x x > (setq z 'x) x

13 X is inserted at the front/top of the list
Functions Math functions +, -, *, /, exp, expt, log, sqrt, sin, cos, tan, max, min Functions for creating lists or ‘List Constructors’ Cons, list and append > (cons 'x L) ; insert symbol x at the front of list L (X 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)) ; appends one list in front of another (A B C D) L is a list containing elements (A,B,C) X is inserted at the front/top of the list

14 Functions (cont’) Selectors first, rest, nth, car, cdr…
> (first '(a s d f)) ;returns the first element of a list a > (rest '((a s) d f)) ;deletes the first element from the ;list and returns the rest as a list (d f) > (first '((a s) d f)) (a s) > (rest '((a s) (d f)) ((d f))

15 > (setq L '(A B C)) (A B C) > (car L) ; returns the first top level element of list L A > (cdr L) ; returns the rest of list L (B C)


Download ppt "Modern Programming Languages Lecture 18 Fakhar Lodhi"

Similar presentations


Ads by Google