Download presentation
Presentation is loading. Please wait.
Published byMoses Fields Modified over 9 years ago
1
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983
2
(REPL) Read-Eval-Print Loop Instead of compiling a program, the coder interacts with an interpreter Interpreter repeats four steps: 1. Read a command 2. Perform operation specified by command 3. Print any results 4. Go to step 1.
3
Functions All commands are functions Called using prefix notation: (function-name arg1 arg2) – Examples: (+ 4 5), (* 2 45) Arguments evaluated from left to right Built-in functions plus user-defined functions
4
FunctionArgsDescription + * 0 or moreAdd, Multiply - / 1 or moreSubtract, Divide 1+ 1- 1Increment, Decrement max min 1 or moreMaximum, Minimum gcd 0 or moreGreatest Common Divisor exp 1e raised to the argument power expt 2Raise 1 st argument to 2 nd argument power log 1 or 2Natural logarithm (base e) or log of 1 st argument base 2 nd arg sqrt 1Square Root abs 1Absolute Value truncate 1 or 2Truncation or division with truncation round 1 or 2Rounding or division with rounding rem 2Remainder random 1Random number in [0,argument) with same type as arg
5
Defining New Functions A Lambda Abstraction is an expression that returns a function: (lambda variable-list term) A term is a syntactically correct LISP expr Returns a function, but not itself a function Based on λ-calculus
6
Lambda >(lambda (x) (+ x x)) (LAMBDA-CLOSURE () () () (X) (+ X X)) >((lambda (x) (+ x x)) 25) 50 >((lambda (x y) (- (+ x x) y)) 2 6) -2 >((lambda (y x) (- (+ x x) y)) 2 6) 10
7
Defun Lambda notation is too tedious for most functions defun is a built-in macro for assigning a function name to a function definition: (defun function-name variable-list term) The function can now be used for the rest of the session
8
Defun Examples >(defun square (x) (* x x)) SQUARE >(square 5) 25 >(defun distance-traveled (v0 s a) "Calculate distance traveled in s seconds by object with acceleration a m/s^2 and initial velocity v0 m/s" (+ (* v0 s) (* 0.5 a (square s)) )) DISTANCE-TRAVELED >(distance-traveled 5 20 5) 1100.0
9
Logic Terms T for true, NIL for false Anything not NIL is considered true in a logical context Predicates: – Functions whose return values are meant to be interpreted as true or false Examples: and, or, not
10
Some Useful Predicates numberp realp rationalp floatp complexp zerop evenp oddp plusp minusp = /= > >= < <=
11
Example Write a function that determines if both of its arguments are positive: (defun both-positive-p (a b) "Determines if both inputs are positive" (and (plusp a) (plusp b)))
12
Conditional Predicates are also important for LISP’s basic conditional function: if (if test then-result else-result) Is a function, because it returns a result (defun if-both-positive-p (a b) "Determines if both inputs are positive using if instead of and" (if (plusp a) (if (plusp b) t nil) nil))
13
Input Files Being able to define functions interactively is great, but we usually want to save our code Fill files with lisp commands that can be loaded from inside the interpreter: (load "filename") Filenames usually have the extension “lisp” – file.lisp
14
Comments Anything following a semi-colon until the end of the line is a comment: (+ x y) ; Adds x and y Block comments created using #| and |# #| This is one big comment (defun both-positive-p (a b) "Determines if both inputs are positive" (and (plusp a) (plusp b))) |# Useful for debugging
15
Data Types Everything in LISP is either an atom or a list The empty list is either nil or () >(equal nil ()) T >() nil Any other list is enclosed in parentheses – More on lists next time
16
Atoms Numbers: integer, rational, float, complex – 5, 5/2, 4.556478, #C(5 1) Strings: "Hello", "My name is" Symbols: – Anything that is not a special character or number ( ) | # ‘ ` ; – Not case sensitive
17
Quote Given any data-expression, return the data-expression, as data (quote data-expression) Abbreviated with a single-quote 'data-expression
18
What’s the point? Quoting gives you the expression itself, rather than the meaning of the expression Explicitly stating that the expression should be treated as data instead of code length of the cobra vs. length of “the cobra” First expression is 4 feet; the second is 9 characters
19
Quote Examples >(quote (1 3 4 5)) (1 3 4 5) >'pi PI >pi 3.1415926535897931 >(quote quote) QUOTE >'(A 4 "Text") (A 4 "Text") Symbols in these examples are 'pi, 'quote and 'a t and nil are special, and therefore do not require quotes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.