# Common LISP-Introduction

Size: px
Start display at page:

Transcription

1 Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally implemented with an interpreter. 3. Evaluation of an s-expression involves performing a computation on it. The result of evaluating an s-expression is called the value of the expression. 4. Simple arithmetic: a. (+ 3 4) b. (* 3 4) c. (* (+ 8 3) (+ 4 (* 9 2))) d. (- 8 3) e. (/ 8 2) f. (/ 8 3) This will give 8/3 as an answer. One value must be floating pt. in order to get a real-valued result. (float (/ 8 3)) also works. g. (* 2 3 4) h. (1+ 4) i. (1 5) 5. Lists are sequences of s-expressions inside matching parentheses 6. Atoms are represented by numbers or symbols (sometimes called literal atoms). Symbols are used to represent variables and function names 7. LISP tries to evaluate every s-expression it sees! After evaluation, the interpreter prints the value that has been computed. 8. Rules of evaluation a. Numbers evaluate to themselves b. Symbols evaluate to the last value assigned to them c. Lists are evaluated by interpreting the first element as a function name and the rest of the list as arguments to that function. The arguments are usually evaluated and the function is then applied to the resulting values. The value returned by the function is the value of the list. Page 1 of 19

2 9. Using the SETQ function a. The setq function as well as the set function is used to perform simple assignment of a value to a symbol. The assignment is global and using setq and/or set is usually frowned upon by most avid LISP programmers. Setq does not evaluate its first argument whereas set does. Some examples follow. b. Examples (setq x 5) (Setq x 5) (setq y (quote (a b c d )_ ) ) 10. Handling errors (setq y (a b c ) ) ; quote is a function of one argument which ; is not evaluated ; This is a shorthand notation for the quote ; function (setq value artificial-intelligence) (set value2 computer-science) (set value3 data structures) ; This assignment would result in an error ; since data-structures has not been ; assigned a value at this point If XLISP gives you an error message, type CTRL-C to return to the top level of the LISP environment. 11. car and cdr a. Examples of car and cdr functions. First and rest can also be used. ( car (a b c )) ( cdr (a b c )) ( setq x (a b c)) (car x ) (cdr x ) x ( car ( cdr (a b))) ( cdr (( a b ) ( c d ))) ( cdr ( car ( cdr (( a b c) ( d e f ))))) b. Using cadr etc. ( car ( cdr (( a b c ) ( d e f )))) ( cadr ((a b c ) (d e f ))) same as directly above ( cadadr ( a b c ) (d e f ))) ( car ( cdr ( a b c ))) Here cdr is just a constant Page 2 of 19

3 c. The empty list ( cdr ( c )) This evaluation will return NIL ( ) This is also NIL ( ) as is this nil this is also NIL cons cons is a function of two arguments. The second argument should always be a list. Cons returns as its value the list obtained by taking the second argument and sticking the first one in front of it. d. ( cons a (b c )) ; The result here is (a b c) e. ( cons a (cons b (c d ))) ; result is (a b c d) f. ( cons a nil) ; result is ( a ) g. (setq x a) h. (setq y (b c)) i. ( cons x y) j. x k. y cons constructs a list and therefore the result requires extra storage. Cons is an expensive operation 12. list and append. List construction functions a. ( list a b c) ; (a b c) b. ( list a (b c) d) ; (a (b c) d) c. (append (a b) (c d) (e f)) ; (a b c d e f ) d. (append (a b) nil) ; (a b) 13. A form is a symbolic expression intended for evaluation. A constant is a form whose value is predetermined. Some special symbols like nil are constants, as are all numbers and quoted expressions. Page 3 of 19

4 Defining functions 1. (defun addthree (x) ( + x 3)) The above defines a simple function that will add three to the integer value passed to it. DEFUN is the function defining function in LISP. It is a special function that does not evaluate any of its arguments. It merely associated the formal parameter list and bodies of code with the function name for future reference. The name of the function above is ADDTHREE. After the function name comes the formal parameter list which in this case just contains x. finally, we have the body of the function which in this case is simply a list representing a call to the + function. LISP functions always return a value. DEFUN is no exception. The value returned by DEFUN is the name of the function, in this case ADDTHREE. However DEFUN is not used here for the value it returns, rather it is used for its side-effect of creating a new function. 2. Define a function xcons that reverses the argument order in a call to the function cons. ( defun xcons (1 e) ( cons e 1)) Call the function: ( xcons (b c) a) ; (A B C) 3. Define a function whose input is a list of two elements and whose output is a list of the list of each element Example >>> ( list-of-lists (a b)) ( (a) (b) ) Definition >> ( defun list-of-lists (x) ( list ( list ( car x)) ( list (cadr x)))) 4. Formal parameters ( setq x (a b c)) ; the value of x is set to a list. This ; assignment is on the top level of LISP ( defun test (x) ; two functions with the formal parameter x ( test2 ( + x 3))) ; are defined ( defun test2 (x) (* x x)) ; now call the function test ( test 5) ; this call will return the value 64 ; however the value of the parameter x will ; still be ( a b c) Page 4 of 19

5 ; LISP does not allow parameters from one function call to interfere with those of ; another, you may use the same symbol as a parameter in as many definitions as ; you like. One way to think about this is that each time a function is called, ; LISP creates a new variable for each of that function s formal parameters. ; Within that function call, all references to a given formal parameter are ; interpreted as references to the variable created just for that function call 5. Free variables, global variables ; A variable designed by a symbol at the top level of LISP is called a GLOBAL ; VARIABLE. (usually denoted by a *name*) ; A variable used within a function, but which is not a formal parameter of that ; function is called a free variable within that function ; An example ( setq sum (a b c)) ( defun sum-average (x y) ( setq sum (+ x y)) ( / sum 2)) ( defun sum-average-caller (sum x y) (sum-average x y) sum) ; now call the function sum-average-caller ( sum-average-caller ) >>> the returned value will be 0 sum the value of the global variable sum will be 120!!! ; The problem of which variable is referenced by the occurrence of a symbol in a ; program is referred to the Scoping Problem ; Essentially there are two types of Scoping: ; Lexical Scoping and Dynamic Scoping ; Common LISP supports lexical scoping and most implementations of common ; LISP are lexically scoped. Having lexical scoping means that formal ; parameters can be referenced only by the code of the functions within which ; they are parameters. ; With dynamic scoping, variables designated by a parameter can be referred to ; outside their functions, and the most recently created variable of a given name is ; used as the reference for a symbol. In the above example, the call to ; ( sum-average-caller ) would return 120 but the value of the global ; variable sum would remain (a b c). Page 5 of 19

6 Predicates, Conditionals, and Logical Operators 1. Predicates are functions that return true or false. In LISP false is indicated by the atom NIL. NIL also represents a valid list expression. Here are some useful predicates a. ATOM returns true if the argument it is passed is an atom. ( atom a) ; true ( atom (a b c)) ; Nil b. LISP returns true if its argument is a list ( listp (a b c)) ( listp (car (a b c))) ; Nil c. NULL returns true if its argument is nil. It returns nil otherwise ( null nil) ; true ( null a) ; false ( null ( ) ) ; true d. EQUAL returns true if two s-expressions look alike ( equal a a) ( equal (a b c) (a b c)) e. NUMBERP returns true if its argument is a number f. Other predicates that take numbers as arguments include ZEROP ODDP EVENP g. MEMBER takes two arguments, the second of which must be a list. MEMBER returns the part of the list of which the mach occurs if its first argument is a member of the list specified as the second argument ( member b (a b c)) ; returns (B C) not true!! ( member b (a (b ) c)) ; returns nil ( member (a b) (a d (a b))) ; returns ( (a b) ) h. There are many more LISP predicates but the above list is representative of the most commonly used predicates. Page 6 of 19

7 2. Conditionals a. The COND function is traditionally used to perform conditional operations b. COND s general form ( cond ( expr11 expr12 expr13 ) ( expr21 expr22 expr23 ) ( expr31 expr32 expr33 ) ( exprn1 exprn2 exprn3 ) ) ; end of cond clause LISP returns the value of the last expression it evaluates as the value of the COND ; let s define a function using cond ( defun test-val (arg) ( cond ((null arg) empty-argument) ((listp arg) (cdr arg)) ( t (arg is an atom)) ) ; end of cond ) ; end of test-val 3. Logical Operators The logical operators include AND, NOT and OR Examples: ( not (atom x)) ; returns true if x is not an atom ( and (evenp x) (< x 100) (evenp y)) ; returns true if all 3 conditions are ; true ( or (null x) (numberp x)) ; returns true if x is null or if x is a ; number Page 7 of 19

8 ASSIGNMENT FORMAT Create a file that will define and test all of the functions as follows: Your Name Problem 1 (defun length (l) This function finds the length of a list of atoms If the list is empty, the function returns zero. ((null l) 0) (t (+ 1 (length (cdr l)))))) Problem 2 (defun drop (n l) The function creates a new list that has the first occurrance of n removed from the list (drop 'a '(x y a b a c)) returns (x y b a c) ((null l) nil) ((equal n (car l)) (cdr l)) (t (cons (car l) (drop n (cdr l)))))) Problem 3 (defun drop-all (n l) The function creates a new list that has the all occurrances of n removed from the list (drop 'a '(x y a b a c)) returns (x y b 1c) ((null l) nil) ((equal n (car l)) (drop-all n (cdr l))) (t (cons (car l) (drop-all n (cdr l)))))) **** End of function definitions **** (dribble 'fname) (print "Testing the function: Length") (pprint (get-lambda-expression (function length))) print the function (print '(length (a b c d))) (print (length '(a b c d))) Page 8 of 19

9 (print '(length nil)) (print (length nil)) ********************************************** Testing drop (print "Testing the drop function") (pprint (get-lambda-expression (function drop))) (print '(drop a (x y a b c a))) test line (print (drop 'a '(x y a b c a))) actual test (print '(drop a (x y z))) (print (drop 'a '(x y z))) ********************************************** Testing drop-all... (dribble) Page 9 of 19

10 THE DO STATEMENT FOR ITERATION Iterative version of finding the length of a list (defun do-length (l) This function finds the length of a list using iteration The DO function performs the iteration. (do define the variables ( (ind1 l (cdr ind1)) first variable (sum 0 (+ 1 sum)) second variable ) end of variable definitions exit clause is next ((null ind1) sum) close exit clause body of Do statement is next ) end of do statement ) end of defun Iterative version of finding length of a list of lists (defun do-len2 (l) This function finds the length of a list using iteration The DO function performs the iteration. (do define the variables ( (ind1 l (cdr ind1)) first variable (sum 0 ((listp (car ind1)) (+ (do-len2 (car ind1) ) sum)) (t (+ 1 sum)))) ) end of variable declarations exit clause is next ((null ind1) sum) close exit clause body of Do statement is next ) end of do statement ) end of defun Page 10 of 19

11 LET STATEMENT EXAMPLE (defun compute (fn) compute takes a function name as an argument and makes a computation using the argument. The lisp statements LET READ and FUNCALL are introduced. (print '(please input two values to compute)) (do ((num 0 (+ 1 num)) ) end of variable declarations ((> num 5)) body of do (terpri) blank out a line ) close do (let ((x (read)) (y (read))) body of let (funcall fn x y)) ) close defun Page 11 of 19

12 PROPERTY LISTS This is an example using property lists object property value animal color brown animal age find-age function dog ako animal fido isa dog fido color black butch isa dog butch color white penny isa dog Declare the semantic network above using property lists (setf (get 'animal 'color) 'brown) (setf (get 'animal 'age) (function find-age)) (setf (get 'dog 'ako) 'animal) (setf (get 'fido 'isa) 'dog) (setf (get 'fido 'color) 'black) (setf (get 'butch 'color) 'white) (setf (get 'butch 'isa) 'dog) (setf (get 'penny 'isa) 'dog) Implement a function to traverse the hierarchy following AKO and ISA links (defun select (obj prop) ((get obj prop) (get obj prop)) ((get obj 'isa) (select (get obj 'isa) prop)) ((get obj 'ako) (select (get obj 'ako) prop)) (t nil))) (print '(select penny color)) (print (select 'penny 'color)) Implement functions as values of properties (defvar *year* 2004) (defun find-age (date) (- *year* date)) Page 12 of 19

13 (setf (get 'animal 'age) (function find-age)) (setf (get 'penny 'yearofbirth) 2000) (funcall (get 'object 'age) (get 'penny 'yearofbirth)) ; (defun selectnew (obj prop) ((get obj prop) (get obj prop)) ((get obj 'isa) (select2 (get obj 'isa) prop obj)) ((get obj 'ako) (select2 (get obj 'ako) prop obj)) (t nil))) (defun select2 (obj prop original) (print obj) (print prop) (print original) ((get obj prop) ((fboundp (get obj prop)) do I have a function to execute? (select3 (get obj prop) prop original)) (t (get obj prop)))) value not found, use inheritance ((get obj 'isa) (select2 (get obj 'isa) prop original)) ((get obj 'ako) (select2 (get obj 'ako) prop original)) (t nil))) (defun select3 (foo prop original) (print prop) (print original) ((equal prop 'age) (funcall foo (get original 'yearofbirth))) (t 'something-is-wrong))) Page 13 of 19

14 MACROS Macros A macro is essentially a function that generates Lisp code. The difference between a macro and a function is that a function produces results whereas a macro produces expressions - which, when evaluated, produce results. Define a simple macro (defmacro pos-x (l) `(cadr,l)) Look at the macro expansion (print (macroexpand-1 '(pos-x (a b c d)))) Call the macro (print (pos-x '(a b c d))) Example #2 --> (defmacro iff(test Then &optional Else) A replacement for cond If the first argument evaluates to non-nil, then evaluate and return the second argument. Otherwise, evaluate and return the third argument (which defaults to nil ` (,test,then) (t,else))) Expand the macro (print (macroexpand-1 '(iff (> 4 3) (print 'greater)))) Use the macro (defun testmacro (l) (iff (null l) nil (print l))) (print (testmacro '(a b c d))) (print (testmacro '() )) End of macro code Page 14 of 19

15 OBJECT-ORIENTED PROGRAMMING create an object-oriented environment for gum-ball machines (defun gum-machine (supply) this function returns a gumball machine supply represents the gumballs contained in the gumball machine (function (lambda () (prog1 (car supply) (setq supply (cdr supply)))))) Now, create a gumball machine (defvar *hyvee-machine* (gum-machine '(red white blue green red black blue))) ***************************************************************** define a function that returns a queue object using the labels function (defun make-queue ( ) (let ((queue nil)) body of let statement (labels ( (empty-queue () (null queue)) (add-queue (v) (setf queue (append queue (list v)))) (dequeue () ((null queue) 'empty-queue) (t (prog1 (car queue) (setq queue (cdr queue)))))) (p-queue () queue) (dispatch (m) ((equal m 'empty?) #'empty-queue) Page 15 of 19

16 ((equal m 'dequeue) (dequeue)) ((equal m 'printq) (p-queue)) ((listp m) ((equal (car m) 'add) (add-queue (cadr m))) (t 'error))) (t 'error)))) end of function definitions body of labels returns dispatch function since it intercepts all messages #'dispatch ) close labels ) close let ) close defun ; (defvar *myqueue* (make-queue)) returns a queue object check if the queue is empty (funcall (funcall *myqueue* 'empty?)) (funcall *myqueue* '(add 10)) (funcall *myqueue* '(add 5)) (funcall *myqueue* 'dequeue) (funcall *myqueue* 'printq) Page 16 of 19

17 USING DEFSTRUCT Define the structure for a binary sort tree (defstruct node val left right) Define the insert function (defun add-tree (root v) Insert one item into the binary tree (let ((x (node-val root))) body of let ((> v x) ((equal (node-right root) nil) (setf (node-right root) (make-node :val v))) (t (add-tree (node-right root) v)))) Must be less than or equal to (t ((equal (node-left root) nil) (setf (node-left root) (make-node :val v))) (t (add-tree (node-left root) v))))))) Define a depth-first tree traversal (defun df (root v) do a depth-first search looking for v at node root ((null root) nil) ((equal v (node-val root)) 'found) ((df (node-left root) v)) (t ( df (node-right root) v)))) Page 17 of 19

18 Page 18 of 19

19 Page 19 of 19

### Functions, Conditionals & Predicates

Functions, Conditionals & Predicates York University Department of Computer Science and Engineering 1 Overview Functions as lambda terms Defining functions Variables (bound vs. free, local vs. global)

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

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

Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

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

1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

### Common Lisp. Blake McBride

Contents Common Lisp Blake McBride (blake@mcbride.name) 1 Data Types 2 2 Numeric Hierarchy 3 3 Comments 3 4 List Operations 4 5 Evaluation and Quotes 5 6 String Operations 5 7 Predicates 6 8 Math Predicates

### INF4820. Common Lisp: Closures and Macros

INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### Lecture Notes on Lisp A Brief Introduction

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

### Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

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

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

Introduction to LISP York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 11_LISP 1 Introduction to LISP Evaluation and arguments S- expressions Lists Numbers

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

LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,

### CSCI337 Organisation of Programming Languages LISP

Organisation of Programming Languages LISP Getting Started Starting Common Lisp \$ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo

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

Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

### A little bit of Lisp

B.Y. Choueiry 1 Instructor s notes #3 A little bit of Lisp Introduction to Artificial Intelligence CSCE 476-876, Fall 2017 www.cse.unl.edu/~choueiry/f17-476-876 Read LWH: Chapters 1, 2, 3, and 4. Every

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017 Agenda 2 Previously Common

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

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

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Recursion & Iteration

Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

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

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Lisp. Versions of LISP

Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major

### Functional Programming

Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

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

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

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

CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

### A Genetic Algorithm Implementation

A Genetic Algorithm Implementation Roy M. Turner (rturner@maine.edu) Spring 2017 Contents 1 Introduction 3 2 Header information 3 3 Class definitions 3 3.1 Individual..........................................

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

6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

### Allegro CL Certification Program

Allegro CL Certification Program Lisp Programming Series Level I Review David Margolies 1 Summary 1 A lisp session contains a large number of objects which is typically increased by user-created lisp objects

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

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

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti L I S P - Introduction L I S P

### Review of Functional Programming

Review of Functional Programming York University Department of Computer Science and Engineering 1 Function and # It is good programming practice to use function instead of quote when quoting functions.

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší

### CS61A Midterm 2 Review (v1.1)

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

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

Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

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

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

Announcements 1 Today s Menu Finish Introduction to ANNs (original material by Dr. Mike Nechyba) > Slides 58-60 > Adjusting the Learning Rate Loose Ends in > Lambda Expressions Review > I/O Functions >

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

CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

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

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

### a little more on macros sort of like functions, but..

a little more on macros 1 sort of like functions, but.. one of the most interesting but tricky aspects of Lisp. unlike functions, macros don't evaluate their arguments; they compute on unevaluated expressions

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

CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

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

Lisp: Question 1 Write a recursive lisp function that takes a list as an argument and returns the number of atoms on any level of the list. For instance, list (A B (C D E) ()) contains six atoms (A, B,

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

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

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

### SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

### Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

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

Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University ACL2 www.cs.utexas.edu/~moore/acl2 ACL2 is a programming language, logic, and theorem prover/checker

### Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Homework Solutions: Problem Set Homework 2 Due uesday September 8, 2015 in class Be sure to follow the guidelines for Programming Assignments. Since these problems are simple, you may skip the Statement

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

Symbols are more than variables So far we have been looking at symbols as if they were variables Symbols and Property Lists Based on Chapter 7 in Wilensky + Prof. Gotshalks notes on symbols > ( setq x

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

λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have

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

SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

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

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial About this Document This document was written to accompany an in-person Lisp tutorial. Therefore, the information on this document alone is not likely to be sufficient

### CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

### Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Racket: Macros Advanced Functional Programming Jean-Noël Monette November 2013 1 Today Macros pattern-based macros Hygiene Syntax objects and general macros Examples 2 Macros (According to the Racket Guide...)

### Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

### 6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### A Quick Introduction to Common Lisp

CSC 244/444 Notes last updated ug. 30, 2016 Quick Introduction to Common Lisp Lisp is a functional language well-suited to symbolic I, based on the λ-calculus and with list structures as a very flexible

### Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

### Introduction to Lisp

Last update: February 16, 2010 Introduction to Lisp Dana Nau Dana Nau 1 Outline I assume you know enough about computer languages that you can learn new ones quickly, so I ll go pretty fast If I go too

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### Functional Programming Languages (FPL)

Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

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

Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

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

Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

### 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 FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

### Functional Programming with Common Lisp

Functional Programming with Common Lisp Kamen Tomov ktomov@hotmail.com July 03, 2015 Table of Contents What is This About? Functional Programming Specifics Is Lisp a Functional Language? Lisp Says Hi Functional

### Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### Principles of Programming Languages

Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

### CSCI 2210: Programming in Lisp. Progn. Block. CSCI Programming in Lisp; Instructor: Alok Mehta 1. ANSI Common Lisp, Chapters 5-10

CSCI 2210: Programming in Lisp ANSI Common Lisp, Chapters 5-10 CSCI 2210 - Programming in Lisp; Instructor: Alok Mehta; 4.ppt 1 Progn Progn Creates a block of code Expressions in body are evaluated Value

### Heap storage. Dynamic allocation and stacks are generally incompatible.

Heap storage Dynamic allocation and stacks are generally incompatible. 1 Stack and heap location Pointer X points to stack storage in procedure Q's activation record that no longer is live (exists) when

### FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti MACROS Introduction to macro system

### Associative Database Managment WIlensky Chapter 22

Associative Database Managment WIlensky Chapter 22 DB-1 Associative Database An associative database is a collection of facts retrievable by their contents» Is a poodle a dog? Which people does Alice manage?»

### Artificial Intelligence Programming

Artificial Intelligence Programming Rob St. Amant Department of Computer Science North Carolina State University Lisp basics NC State University 2 / 99 Why Lisp? Some recent Lisp success stories include

### Associative Database Managment

Associative Database Managment WIlensky Chapter 22 DB-1 Associative Database An associative database is a collection of facts retrievable by their contents» Is a poodle a dog? Which people does Alice manage?»

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%

### Introduction to Functional Programming and basic Lisp

Introduction to Functional Programming and basic Lisp Based on Slides by Yves Lespérance & Peter Roosen-Runge 1 Functional vs Declarative Programming declarative programming uses logical statements to

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

Department of Computer and information Science Norwegian University of Science and Technology http://www.idi.ntnu.no/ A Crash Course in LISP MNFIT272 2002 Anders Kofod-Petersen anderpe@idi.ntnu.no Introduction

### The Typed Racket Guide

The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

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

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

### Documentation for LISP in BASIC

Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have