Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Similar documents
Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Lisp. Versions of LISP

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Lists in Lisp and Scheme

Common Lisp. Blake McBride

Imperative, OO and Functional Languages A C program is

Common LISP-Introduction

15 Unification and Embedded Languages in Lisp

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CSc 520 Principles of Programming Languages

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Lecture #2 Kenneth W. Flynn RPI CS

Lecture Notes on Lisp A Brief Introduction

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Introduction to Functional Programming and basic Lisp

Project 2: Scheme Interpreter

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Common LISP Tutorial 1 (Basic)

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

CS 314 Principles of Programming Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

A Quick Introduction to Common Lisp

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

Introduction to Scheme

Intro. Scheme Basics. scm> 5 5. scm>

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Functional Programming. Pure Functional Languages

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Functional Programming. Pure Functional Languages

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

CSCI337 Organisation of Programming Languages LISP

Functional Programming. Pure Functional Programming

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional programming with Common Lisp

Heap storage. Dynamic allocation and stacks are generally incompatible.

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Functional Languages. Hwansoo Han

User-defined Functions. Conditional Expressions in Scheme

Department of Computer and information Science Norwegian University of Science and Technology

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Reasoning About Programs Panagiotis Manolios

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

LECTURE 17. Expressions and Assignment

A Small Interpreted Language

Lisp Basic Example Test Questions

A Brief Introduction to Scheme (II)

LECTURE 16. Functional Programming

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

A Genetic Algorithm Implementation

Allegro CL Certification Program

ALISP interpreter in Awk

FP Foundations, Scheme

Introductory Scheme. Revision 1

Scheme: Strings Scheme: I/O

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

CSC 533: Programming Languages. Spring 2015

Functional Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Associative Database Managment WIlensky Chapter 22

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Associative Database Managment

SOFTWARE ARCHITECTURE 6. LISP

CS 314 Principles of Programming Languages. Lecture 16

CS 360 Programming Languages Interpreters

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,


Functional Programming Lecture 1: Introduction

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Documentation for LISP in BASIC

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

Data abstraction, revisited

Lecture #24: Programming Languages and Programs

CS 314 Principles of Programming Languages

Vectors in Scheme. Data Structures in Scheme

COP4020 Programming Assignment 1 - Spring 2011

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

An Explicit-Continuation Metacircular Evaluator

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

CSE 341 Section Handout #6 Cheat Sheet

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Full file at

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Functional Programming Languages (FPL)

SCHEME AND CALCULATOR 5b

Introduction to Lisp

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Artificial Intelligence Programming

Transcription:

LISP NOTES #1 LISP Acronymed from List Processing, or from Lots of Irritating Silly Parentheses ;) It was developed by John MacCarthy and his group in late 1950s. Starting LISP screen shortcut or by command clisp Quitting LISP command (exit) Exiting debugging command abort LISP computations = evaluation of symbolic expressions (s-expressions) LISP is not case sensitive and converts everything to upper case at the time of reading the input. S-Expressions (operation arg1 arg2 argn) or atom S-expression is short for symbolic expressions. Operation is a function and some take fixed and some variable number of arguments. An atom is a number or a symbol, these are elements that do not have parts. The LISP Evaluation Rule Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator. Note: the parentheses must be balanced in an s-expression or you get an error. Note: numbers, symbol t, and symbol nil evaluate to themselves. Tip: use new lines and indentation to make the code more readable and to keep track of parenthesis balance. Example (* (+ 8 3) (+ 4 (* 9 2))) 242 Note: (+ 8 3) is a supplied argument to the operator *, the actual argument to the operator * is 11 which is the result of evaluating (+ 8 3).

Symbols Symbols in LISP are the equivalent of variables. Values are associated with symbols using the function SETQ (setq x 5) 5 x 5 Note: Value of a symbol is the value associated (assigned to) with it in the most recent assignment. Note: SETQ is a special function; it does not entirely follow the LISP evaluation rule since it does not evaluate the first argument given to it and takes it literally. There are only a few special functions. LISP is very open-minded about the symbol names (setq +8-11) 11 +8-11 Symbols have a multiple nature; they can be associated with a value and they can beassociated with function at the same time. (1+ 8) 9 (setq 1+ 6) 6 (1+ 1+) 7 Note: it is a bad programming practice to use a symbol for both at once : a variable and a function. Note: value of some symbols has a special meaning (e.x. + * numbers) and the redefinition of these is considered a bad programming practice or even an error. LISP Data Types LISP has a very rich set of data types: all the standard types, as well as, of example fractions and complex numbers. (setq a 1/3) 1/3 (/ a 3) 1/9

(* a 3) 1 Rational numbers: integers and fractions. Floating-point numbers - decimal notation (e.x. 18.07) or scientific notation (1.807E1) - many precisions can be specified (just replace the E in the scientific notation) o S short o F single o D double o L long LISP numeric results can be extremely large and with perfect precision without any effort: (setq x (* (* 123 234 345 456 567 678 789 890) (* 123 234 345 456 567 678 789 890))) 1494064471333012358243042404455831452160000 (* x x x x x x x x x x) 55423428020747273690519751468789783948416909183409298837078561985873451076829525 85968355154059567534903568146532517998603860229004641824226415041741959754212698 45291959738176656410958822212681883799912723039083300124469795053149670785589142 70312171782422257631720057881692450141805357390153654166881384612018259520588426 72227658129925375678538077172446733936799257103939268065099776000000000000000000 0000000000000000000000 Lists (a (b c) d) = list of 3 elements, the second is a list of 2 elements (((x))) = list of one element which is a list of one element which is a list of one element x Symbolic Operations Arguments are s-expressions, operation is applied to such arguments, and the result is an s-expression. The result is the same sort of object as the arguments that are evaluated. Note: we may need to suppress argument evaluation to pass an s-expression to a function, otherwise the s-expression would be evaluated and such result would be passed. This is dome using the (QUOTE x) function or x to shorten the notation (i.e. syntactic shortcut). QUOTE is a special function its argument is not evaluated and it is returned back. Note: (QUOTE 6) i.e. 6 does not do anything is is equivalent to just 6 since numbers can always be evaluated and they evaluate to their value (setq x (+ 3 4)) 7

(setq x (+ 3 4)) (+ 3 4) Note: there is an ordinary function SET which is equivalent to SETQ. It evaluates its first argument. (SETQ X Y) is equivalent to (SET X Y). SET is hardly used. (setq b 5) 5 (setq a b) b (set a 6) 6 b 6 Forms are s-expressions intended for evaluation (not for manipulation) Symbolic operations take lists apart and build them up again to produce results. CAR function returns the 1 st element of a list i.e. return head CDR function returns the list without the 1 st element of a given list i.e. skip head CAR and CDR are non destructive and return a newly constructed structures. (car (a b c)) a (cdr (a b c)) (b c) CAR and CDR can be composed and abbreviated (car (cdr (cdr (a b c)))) c (caddr (a b c)) c Note: program code can be handled the same as data in LISP. You can manipulate code: (car (cdr (a b c))) cdr

Empty List aka NIL An empty list (), also represented equivalently as NIL, is a special symbol in LISP. It evaluates to itself by convention so it does not need quoting. You cannot change its value. It also has a special meaning in LISP (similar to NULL in Java). () nil () nil nil nil (cdr (a)) nil Constants A constant in LISP is: - NIL - Number - Quoted s-expression List Construction A list can be constructed from head and tail (which must be a list) by the CONS function i.e. add the first element to a list. CONS is non destructive and returns a newly constructed structure. (cons a (b c)) (a b c) (car (cons a (b c))) a (cdr (cons a (b c))) (b c) You can also construct a list from individual elements using the LIST function (list a c e g) (a c e g) Or you can append several lists into one (append (a b c) (1 2 3)) (a b c 1 2 3) Note: (cons (a b) (c d)) produces ((a b) c d))

Note: (cons a nil) produces (a) Internal List Representation Regular LISP structures are represented internally as binary trees (although you can build any graph with some special functions). The nodes, also called cons cells or conses, have a right pointer and a left pointer and the terminal nodes are atoms. The node pointed by the left pointer can be obtained by using the CAR function, and the node pointed by the right pointer can be obtained by using the CDR function. The CONS function constructs a tree node and sets the left pointer to the first element and the right pointer to the second element. (cons a b) produces a cons cell that is denoted by a dotted pair (a. b), if b is nil then. nil can be omitted in the denotation, thus (cons a nil) is denoted (a) Every denotation of LISP list has an equivalent dotted pair denotation, but not all dotted pair denotations have a list denotation. The list notation is just for human convenience but the dotted notation better reflects the internal representation. (a b c) (a. (b. (c. nil))) Dotted notation is dangerous; you can create non list objects, (a. b) is not a list unless b is nil. CAR and CDR work on conses. You can use the CONSP function to check if an object is a cons cell. Note: LISTP does what CONSP does plus classifies NIL as a list, it does not check if a given object is a proper list structure. (listp (a b)) (consp (a b)) (listp (a. b)) (consp (a. b)) (listp nil) (consp nil) NIL Note: NIL is a list but not cons!!! Note: List manipulation is really a tree manipulation. Equality (setq x (a b c))

(setq y (a b c)) (setq z x) (equal x y) (eq x y) NIL (eq x z) EQUAL tests it two structures are equivalent, EQ tests if two symbols refer the same structure in memory. Note: there is no guarantee that referencing a numeric value (ex. 5 and 5) will point to the same atom thus (eq 5 5) may sometimes not work, therefore to test if two symbols point to the same object or the same value of the same type you should use EQL function. Note: EQ and EQL work for symbols i.e. (eq a a) Destructive List Manipulation There are functions that will manipulate the memory directly rather than build a new object and return it. They are dangerous and power full because they allow building any graph structures including circular ones. RPLACA substitutes for the left pointer of a cons cell RPLACD substitutes for the right pointer of a cons cell (setq x (a b c)) (setq y (cdr x)) (rplaca y c) x (a c c) (rplacd y d) x (a c. d) The destructive list manipulation functions are very powerful (you can do things that no other functions can do) and very efficient. NCONC glues two lists destructively APPEND creates a new list and by copying and glueing the given lists Property Lists Symbols can have a list of properties associated with them in addition to their value and the function.

(get chair3 color) retrieves a property (a pointer to a property actually) COLOR of symbol CHAIR3 (setf (get chair3 color) red) sets the property COLOR of object CHAIR3 to RED (get chair3 color) NIL (setf (get chair3 color) red) RED (get chair3 color) RED (setf (get chair3 owner) john) JOHN The properties of a symbol are stored as a list (key1 val1 key2 val2 keyn valn), the GET function may return NIL that indicates that the property is indeed NIL or that it has not been stored yet. To avoid this confusion use some sentinel value to initiate a property and if GET returns such sentinel then you know it is set but to the UNSPECIFIED value. Note: Properties were used in the past to do Knowledge Representation, where symbols represented real-world objects. Note: Property lists are global, if you set a property for a local variable you really set it globally for the symbol used as a local variable. Unattached Property List You can define a list of the form (key1 val1 key2 val2 keyn valn) and handle it just like symbol property lists. (setq plist (john ford mary kia alan bmw)) (john ford mary kia alan bmw) plist (john ford mary kia alan bmw) (getf plist mary) kia (setf (getf plist mary) gm) gm plist (john ford mary gm alan bmw) Note: SETF may change the sequence of elements but the key-value association is preserved.