Download presentation
Presentation is loading. Please wait.
Published byCharles Dawson Modified over 8 years ago
1
LISP LISt Processing
2
History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later adopted as the primary research language of Artificial Intelligence (AI). b b The syntax of LISP is one of the least restrictive of any high- level programming languages.
3
History & Overview b b Was originally developed as an interpreted language, however, a number of current versions of LISP make use of an incremental compiler. b b Among LISP's strongest features is that programs may be treated as data. b b In LISP, entire programs can be used as input to other programs or sub-procedures directly.
4
Grammar List :: = ( [X 1 X 2 … X n ]) List :: = ( [X 1 X 2 … X n ]) X :: = List | Atom X :: = List | Atom Atom :: = Number | Name Atom :: = Number | Name Number :: =... | -1024 |... | -2 | 1 | 0 | 1 | 2 |... | 1024 |... Number :: =... | -1024 |... | -2 | 1 | 0 | 1 | 2 |... | 1024 |... Name :: = a sequence of alphanumeric characters containing at least one letter and no separators Name :: = a sequence of alphanumeric characters containing at least one letter and no separators Separator :: =. | space | ( | ) | [ | ] | ‘ | tab | carriage return all are characters with a particular role. Separator :: =. | space | ( | ) | [ | ] | ‘ | tab | carriage return all are characters with a particular role.
5
Features/Parts of LISP Definitions b Atom : A series of numbers or symbols that can’t be broken in to smaller components. Ex. Cat, Scott9, 684, 7Hearts, This-Is-A-Very-Long-Atom b List : A sequence of atoms or other lists enclosed by parentheses LISTLENGTH b () 0 (John gives Mary a book) 5 ((subject) (verb) (object)) 3 ((+ X Y) (+ A B)) 2 (1 2 3 A B C C B A 3 2 1) 12 (()()) 2
6
Features/Parts of LISP Syntax b The basic syntax for LISP is : (expression) b Everything is expressed as a list or as part of a list, enclosed in parentheses b Not case-sensitive b Supports recursion
7
Features/Parts of LISP Syntax b The comment character is a semi-colon ‘;’ which can appear anywhere. This results in the remainder of the line being skipped. b There are 2 main components of a list, the head of the list and the rest of the list. The head of the list or the first list item is referred to as CAR. The rest of the list is referred to as CDR. (Note: CAR & CDR were register names from the IBM mainframe that LISP was first written for.)
8
Features/Parts of LISP Syntax b Many times the CAR is a function with its arguments being CDR. The basic mathematical operations are a good example. (+ 4 2) (- 4 2) (* 4 2) (/ 4 2) b Note the syntax for all functions is simply a list of symbols and numbers. b All list operations return a value. This value is the last value in the list. For a procedure, the last value is the return or result value.
9
List Manipulation Some of the LISP primitives (built-in commands or functions) for manipulating lists are: b LIST - Returns a list of its arguments > (list 1 2 3 4) SETQ - Takes at least 2 arguments, the 1st being a variable name and the 2nd being any LISP expression. LISP evaluates the expression passed as the 2nd argument and assigns the value returned as the new value of the variable passed as the 1st argument. > (setq mylist (list 1 2 3 4)) > (setq yourlist (list 10 11 12 13)) b SETQ - Takes at least 2 arguments, the 1st being a variable name and the 2nd being any LISP expression. LISP evaluates the expression passed as the 2nd argument and assigns the value returned as the new value of the variable passed as the 1st argument. > (setq mylist (list 1 2 3 4)) > (setq yourlist (list 10 11 12 13)) (1 2 3 4) (1 2 3 4) (1 2 3 4) (10 11 12 13)
10
List Manipulation b CAR - Returns the first element of a list > (car mylist) 1 b CDR - Returns a list of all elements except the first > (cdr mylist) (2 3 4) (2 3 4) b Nth - Returns a specific list element > (nth 3 mylist) > (nth 2 yourlist) 4 12 12
11
List Manipulation b CONS - Short for concatenates. Will insert a new element at the front of a list > (cons (car mylist) (cdr mylist)) > (cons (car mylist) (cdr yourlist)) (1 2 3 4) (1 11 12 13) > (cons mylist yourlist) ((1 2 3 4) 10 11 12 13)
12
List Manipulation b APPEND - Adds new list elements to the end of a list > (append mylist yourlist) (1 2 3 4 10 11 12 13)
13
Functions b DEFUN - This command is used to define functions which LISP refers to as a value-returning procedures. > ( defun MyValueReturningProcedureName (optional_input_variables) ValueReturningProcedure_body )
14
Input/Output b PRINT & PRINC - These will output an expression to the output device followed by a space. PRINT adds a line-feed. b READ - This will read an expression from the input device.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.