140 likes | 304 Views
Data Structures. S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S-Expression enclosed by (…) Atom - String of characters beginning with a letter, digit. Eg: Artificial, intelligence, 31416..etc. Symbols & Lists.
E N D
Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S-Expression enclosed by (…) Atom - String of characters beginning with a letter, digit. Eg: Artificial, intelligence, 31416..etc. TES3111 Oct 2001
Symbols & Lists • A symbol is just a string of characters, made up of letters, digits and hyphens. Examples of symbols: jack, jill, what-is-that … etc. • Lists consists of zero or more elements enclosed in parenthesis. It looks like this: ‘(element1 element2 …. Elementn) TES3111 Oct 2001
Symbols and Lists • Caution! - Lists must be preceded by a single quote ‘. Otherwise Lisp will evalute the whole expression as a function invocation. • Examples of lists: ‘(what day “is today”) ‘(this (is a) nested list) • To construct a list, do as use the list function as follows: (list ‘(a b c) (* 2 3 5)) • An empty list is represented using () or nil. TES3111 Oct 2001
Some Lisp Functions • (setq x 5) ; assign the value 5 to x • (let ((x 100) x) ; temporarily bind x to 100 • (* a 9) Note that the semi-colon is use to insert comments. It is not the end of statement indicator as in other languages. TES3111 Oct 2001
Boolean values • True is represented as t and False is represented as nil . Examples of conditionals: (if t “Hello” “Wrong”) (if nil 10 100) (if 1 2 3) TES3111 Oct 2001
Keywords • Keywords are self evaluating symbols. We have already encountered 2 of this kind - t and nil. • Any symbols that begins with a colon is a keyword. Examples: :lisp-name-space :what-is-lisp TES3111 Oct 2001
Numbers • Lisp provides 4 types of numbers: • Integers: 12, 9, -12 • Rational numbers: 12/87, 987/10 • Real numbers: 3.142, -10.987, 1.5567e-20 • Complex numbers in the form of #c(r i). Where r is the real part and i is the imaginary part. Examples: #c(1.65, 0.6) TES3111 Oct 2001
Conversion & Extractions • There are functions for converting and extracting components of the 4 kind of numbers. (float 23) ; convert integer to float (truncate 23.456) ; (floor 56.234) (ceiling 34.567) (round -34.023) TES3111 Oct 2001
Arithmetic • Please be reminded that Lisp uses the prefix notation for function invocation. x - y - z, in Lisp this is (- x y z) (a * b) + (c*d), in Lisp this is (+ (* a b) (* c d)) (/ 24 7) (+ 1 2 3 4 5 6 7 ) • Be careful of how you nest the operators. TES3111 Oct 2001
Conses • Try these out: (CAR ‘(a b c)) (CAR (CAR ‘(a b c)) (CDR ‘(a b c)) (CAR (CDR ‘(a b c)) What do these 2 functions do ? • A cons is a 2-field record made up of car and cdr. (cons 23 24) (cons (cons 1 2) 3) (car (cons 9 10)) (cdr (cons 10 9)) TES3111 Oct 2001
Using lists as stacks • How? By storing a list in a variable and using 2 functions: push and pop. (setq a nil) ; start with an empty stack (push 1 a) (push 2 a) (pop a) (pop a) TES3111 Oct 2001