Lecture Notes on Lisp A Brief Introduction

Similar documents
Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Lisp Basic Example Test Questions

A little bit of Lisp

Common Lisp. Blake McBride

Imperative, OO and Functional Languages A C program is

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

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

Common LISP Tutorial 1 (Basic)

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

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

Common LISP-Introduction

CSCI337 Organisation of Programming Languages LISP

Functional programming with Common Lisp

Functions, Conditionals & Predicates

Recursion & Iteration

Review of Functional Programming

CSC 533: Programming Languages. Spring 2015

A Brief Introduction to Scheme (II)

Lisp. Versions of LISP

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

Artificial Intelligence Programming

Functional Programming. Pure Functional Languages

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

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

Functional Programming. Pure Functional Languages

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

Allegro CL Certification Program

Scheme Quick Reference

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

FP Foundations, Scheme

A Quick Introduction to Common Lisp

Building a system for symbolic differentiation

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

Functional Programming. Big Picture. Design of Programming Languages

Scheme Quick Reference

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

Functional Programming

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

Documentation for LISP in BASIC

Announcements. Today s Menu

CS61A Midterm 2 Review (v1.1)

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Func%onal Programming in Scheme and Lisp

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

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

Functional Programming. Pure Functional Programming

CS 314 Principles of Programming Languages

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

Func%onal Programming in Scheme and Lisp

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

Lecture #2 Kenneth W. Flynn RPI CS

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

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

A Genetic Algorithm Implementation

An Explicit-Continuation Metacircular Evaluator

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

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

CSc 520 Principles of Programming Languages

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Building a system for symbolic differentiation

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

Introduction to Scheme

ALISP interpreter in Awk

Introduction to Functional Programming and basic Lisp

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

15 Unification and Embedded Languages in Lisp

CS 314 Principles of Programming Languages. Lecture 16

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

UMBC CMSC 331 Final Exam

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

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Introduction to Lisp

CS 314 Principles of Programming Languages

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

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

SCHEME AND CALCULATOR 5b

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

Allegro CL Certification Program

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

Debugging in LISP. trace causes a trace to be printed for a function when it is called

INF4820. Common Lisp: Closures and Macros

User-defined Functions. Conditional Expressions in Scheme

Introduction to Functional Programming

Midterm Examination (Sample Solutions), Cmput 325

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

by the evening of Tuesday, Feb 6

Heap storage. Dynamic allocation and stacks are generally incompatible.

Symbolic Computation and Common Lisp

Lecture #24: Programming Languages and Programs

1.3. Conditional expressions To express case distinctions like

Functional Languages. Hwansoo Han

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

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

LECTURE 16. Functional Programming

Transcription:

Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham article) Because it s got lots of features other languages don t Because you can write new programs and etend old programs really, really quickly in Lisp Lisp stands for LISt Process Invented by John McCarthy (5) Simple data structure (atoms and lists) Heavy use of recursion Interpretive language Variations Frantz Lisp (0 s) Common Lisp (de facto industrial standard) Common Lisp at glumbcedu and sunservercseeumbcedu command line: clisp main site: http://clispsourceforgenet/ help site: http://wwwapljhuedu/~hall/lisphtml tutorial site: http://grimpeurtamuedu/~colin/lp/node0html Valid objects (S-epressions) Atoms: numbers: (real 0, integer ) symbols: a consecutive sequence of characters (no space) eg, a,, price-of-beef two special symbols: and for logical true and false strings: a sequence of characters bounded by double quotes eg, "this is red" (Note: LISP is case insensitive) Lists: a list of atoms and/or lists, bounded by "(" and "), eg, (a b c), (a (b c top elements of a list eample: 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: empty list, same as () Function calls also a list use prefi notation: (function-name arg argn) returns function value for the given list of arguments functions are either provided by Lisp function library or defined by the user Eamples: >(+ 5) >(/ 5) /5 >(/ 5) 05 >(sqrt 4) Evaluation of S-epression ) Evaluate an atom numerical and string atoms evaluate to themselves; symbols evaluate to their values if they are assigned values, return Error, otherwise; the values of and are themselves ) Evaluate a list - evaluate every top element of the list as follows, unless eplicitly 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 Eamples >(+ (/ 5) 4) /5 >(+ (sqrt 4) 40) 0 >(sqrt ) Error: he variable X is unbound

) o assign a value to a symbol (setq, set, setf) >(setq ) > 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-epression, which will be evaluated; the value of the second argument is assigned to be the value of the first argument >(setq y ) ; the value of is assigned as the value of y >y to forbid evaluation of a symbol (quote or ) >(+ y) 0 >(quote ) >' >(+ z) Error: X is not of type NUMBER >(setq z ') to force an evaluation, using function "eval" >(+ (eval z 0 wo more assignment functions: (set y) ; assign the value of y to the value of is evaluated ; first and whose value must be a symbol ; "setq" is a combination of "set" and "quote" (setf y) ; similar to but more general than "setq" in that can be ; something other than a symbol 4 Basic LISP functions ) list operations: car and cdr >(setq L '(a b c (a b c) ; assigns a list (a b c) as the value of L >(cadr L) B ; car of cdr of L >(car L) A ; returns the first top level element of list L >(cddr L) (C) >(cdddr L) (nth i L) returns the ith top element of L (the front element is considered 0th element) >(cdr L) (B C) ; returns the rest of list L >(caddr L) C >(cadddr L) >(nth L) C other list operations >(cons ' L) ; insert symbol at the front of list L (X A B C) >(list 'a 'b 'c) ; making a list with the arguments as its elements ; if a, b, c have values, y, z, then (list a b c) ; returns list ( y z) >(append '(a b) '(c d (A B C D) ; appends one list in front of another >(reverse L) ; reverses a list (C B A) >(length L) ; returns the length of list L ) Predicates (a special function which returns if the predicate is false, or anything other than, otherwise) =, >, <, >=, <= for numerical values; equal, eq, for others (symbols, lists, etc) >(< y) tests if is a atom tests if is a list >(= y) >(listp ) also numberp, symbolp, null >(numberp ) >(equal y) >(atom ) >(numberp ) >(atom L) >(listp L) >(symbolp ) >(equal a (car L >(atom (car L >(symbolp ) >(null L) >(null ) >(null ) ) Set operations ( a list can be viewed as a set whose members are the top elements of the list) >(member 'b L) ; test if symbol b is a member (a top element) of L (B C) ; if yes, returns the sublist of L starting at the ; first occurrence of symbol b >(member b (cons 'b L (B A B C) >(member L) ; if no, returns >(union L L) ; returns the union of the two lists >(intersection L L) ; returns the intersection of the two lists >(set-difference L L) ; returns the difference of the two lists

4) Conditional >(cond (<test-> <action->) (<test-k> <action-k> each (<test-i> <action-i>) is called a clause; if test-i (start with i=) returns (or anything other than ), this function returns the value of action-i; else, go to the net clause; usually, the last test is, which always holds, meaning otherwise cond can be nested (action-i may contain (cond 5 Define functions (heavy use of recursive definitions) (defun func-name (arg- Arg-n) func-body) eamples: (defun member ( L) (cond ((null L) nil) ; base case : L is empty ((equal (car L L) ; base case : =first(l) (t (member (cdr L) ; recursion: test if is in rest(l) (defun intersection (L L) (cond ((null L) nil) ((null L) nil) ((member (car L) L) (cons (car L) (intersection (cdr L) L) (t (intersection (cdr L) L Eample: (intersection '(a b c) '(b a b c returns (a b c) (intersection '(b a b c) '(a b c returns (b a b c) (defun set-difference (L L) (cond ((null L) nil) ((null L) L) ((not (member (car L) L (cons (car L) (set-difference (cdr L) L) (t (set-difference (cdr L) L Define functions iteratively (dolist ( L result) body) for each top level element in L, do body(); is not equal to an element of L in each iteration, but rather takes an element of L as its value; (dotimes (count n result) body) ; do body n times count starts with 0, ends with n- Note: result is optional, to be used to hold the computing result If result is given, the function will return the value of result, returns, otherwise (may change global variables as side effects) (defun sum (L) (dolist ( L y) (setq y (+ y (defun sum (L) (dotimes (count (length L) y) (setq y (+ y (nth count L) (defun sum (L) (dolist ( L y) (setq y (+ y (eval ) defun sum4 (L) (dotimes (count (length L) y) (setq y (+ y (eval (nth count L >(setq L '( ( ) >(dotimes (count ) (set (nth count L) (nth count L) >(sum L) >(sum L) >(setq L '(a b c >(sum L) Error: >(sum L) Error: >(sum L) >(sum4 L) Other functions in LISP library ) Predicates:zerop, plusp, evenp, oddp, integerp, floatp ) Logical connector: and, or, not ) Rounding: floor,ceiling, truncate, round 4) Others: ma, min, abs, sqrt, + (add ), - (minus ) (ep number) (base-e eponential) (ept Base-number Power-Number) (log number & Optional base-number) (isqrt number) Returns the greater integer less than or equal to the eact positive square-root of the number (signum number) Returns -, zero, or according if the number is negative, zero, or positive

7 Other features ) Property lists: Assign/access properties (attribute-value pairs) of a symbol o assign a property: (setf (get object attribute) value) o obtain a property: (get object attribute) Eample: >(setf (get 'a 'heights) ) ; cannot use "setq" here >(get 'a 'height) >(setf (get (cadr L) 'height) ) >(get 'b 'height) ) Associative list: attach a list of properties to a symbol, each property can be retrieved by key (property symbol) >(setf sarah '((height ) (weight 00) (se "F") ((HEIGH ) (WEIGH 00) (SEX "F" >(assoc 'weights sarah) (WEIGH 00) ) mapcar: (mapcar # p-name L) transform list L to another list by performing procedure p-name to each top level element of L >(mapcar # sqrt L) ( 445 7050) (defun sq () (* >(mapcar # sq L) ( 4 ) >(mapcar # set L L) ( ) >(mapcar #'* L L L) ( 7) transforming more than one lists define the function within mapcar (unnamed), use lambda notation >(mapcar #'(lambda () (setq (+ (eval L) ( 4) 4) input/output: print/read on screen: >(print (get 'a 'height >(print L) >(setq p (read 0 ;typed on the screen 0 >p 0 with eternal file: (with-open-file (<stream-name> <file-name> :direction :input or :output) ) internal variable name eternal file name >(with-open-file (data "indat" :direction :input) ; input file indat contains (setq L nil) ; 4 5 (dotimes (count 5) (setq L (cons (read data) L) ) >L (5 4 ) >(with-open-file (result "outdat" :direction :output) (dotimes (count 5) (print (+ (nth count L result) ;an eternal file "outdat" is created and contains 5 4 5) Some new primitive/functions Access a list first, second,, tenth ;etension of CAR, ;return the ith element rest, last ; etension of CDR, return a list Conditional (if <test> body body) (when <test> body) (unless <test> body) ;do body if test is true, ;body, otherwise ;do body when test is true ;do body when test is false 4

) Miscellaneous %clisp >(bye) or (quit) or <ctrl>-d (load "file-name") (ed "file-name") ; enter Common Lisp of CMU (on glumbcedu) ; eit CLISP ; load in a file ; enter vi editor (compile-file "file-name") ; the compiled version is in file-nameo ; then load in file-nameo (compile 'func-name) ; compile a particular function (time (func-name arg argn ; print real and run time for eecuting func-name Summary Atoms and lists Functions and function calls setq, setf, set, quote, eval, math functions (+, -, *, /, ma, min, ep, sqrt, ) list operations: list, cons, car, cdr, length, nth, append, reverse predicates (=, >, equal, eq, numberp, symbolp, ) Defining functions (defun func_name (arg_list) func_body) dolist, dotimes, cond, if, when, unless, mapcar Properties and associative lists: get, assoc Input/output: print, read, with-open-file, load 5