TUTORIAL 3 CSCI3230 (2014-2015 First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
It is suggested that you use the Lisp interpreter available on the general machine (general.asu.edu). You are welcome to use other interpreters while developing,
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
Yu-Tzu Lin ( 林育慈 )
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Using Harlequin LispWorks. Using LispWorks On BURKS 5, the file is lwper410.exe Start up LispWorks; you get two windows –Listener 1 –LispWorks Personal.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
TUTORIAL 1 CSCI3230 ( First Term) By Tony YI 1.
Common lisp A functional programming language. Useful URL:
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Basic Introduction to Lisp
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Getting Started with Lisp
LISP LISt Processing.
LISP A brief overview.
First Lecture on Introductory Lisp
The Metacircular Evaluator
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Common Lisp II.
Presentation transcript:

TUTORIAL 3 CSCI3230 ( First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on

Outline Lispbox Setup IDE Example Short Review Guided Practice Programming Exercises 2

Lispbox 1. Download from 2. Open Lispbox Messages and instructions User input and interpreter output 3

Hello World 4

LISP IDE A prompt for you to enter a LISP expression when CL started: CL-USER> REPL: Read-evaluate-print loop Interactive: at any time you can try your expressions CL-USER> (cos (/ pi 4)) D0 CL-USER> "hello world" "hello world" CL-USER> Read Evaluate Print 5 Q: How is s-expression evaluated? [See Tutorial 1: Control the Flow of Evaluation]

How to Edit, Save, Load and Compile Edit Emacs from Lispbox (Ctrl-h t for a tutorial) Any other text editors Save e.g., testing.lisp (or.cl) Load (load "testing.lisp") Compile (compile-file "testing.lisp") 6

Example 1 mysum.lisp (defun mysum (x y) ;"Sum any two numbers after printing a message." ;printf("Summing %d and %d.\n", x, y) (format t "Summing ~d and ~d.~%" x y) (+ x y)) Lisp IDE > (load “mysum.lisp");Load the mysum.lisp to the environment #P"d:/Software/lispbox-0.7/mysum.lisp" > ( mysum 10 2);Test your function Summing 10 and

Example 2 testing.lisp (defun sum_square (n) (let ((r 0)) (do ((i 1 (+ i 1))) ((= i n) r) (setq r (+ r (* i i)))))) Lisp IDE > (load "testing.lisp");Load the testing.lisp to the environment #P"d:/Software/lispbox-0.7/testing.lisp" > (sum_square 10);Test your function 285 8

Try (sum_square) Click 3 to kill OR Press key 3 to abort OR Press key a to abort 9

Emacs Split the buffer to two vertical frames Ctrl-X 3 Open a file in a buffer Click the open button OR Ctrl-X f Enable Parentheses Match Highlighting Save the buffer Click the save button 10

SLIME The Superior Lisp Interaction Mode for Emacs 11

REVIEW 12

Atom The most basic (indivisible) unit in LISP Any combination of characters, except "(" and ")", can be an atom. For an atom with " "(whitespace) character, it needs to be written as |an atom is here|. 3 types of Atom: Symbols Not case sensitive. E.g. John, abc, 23-Jordan Numbers E.g. 123, 0 3/4 ; rational number #C(3 4) ; complex number = 3 + 4j #C(3/4 4) ; complex number = j != 3/4 + 4j Constants (self-evaluating) Symbols that have special meaning E.g. NIL, T ; Comment here 13

List A non-atomic s-expression. A collection of atom or list enclosed by parentheses ( ). (Jordan 23); a list of atoms "Jordan" and "23" (Jordan (3/4 23)); a list of atoms Jordan and a list of "3/4" and "23" ( ) ; a null list 14

Symbolic-Expression An s-expression is defined recursively: 1. An atom is an s-expression 2. If s 1, s 2, …, s n are s-expressions, then so is the list (s 1 s 2 … s n ). 15

NIL and T NIL An special list called the null (empty) list ( ) Also an atom Means "false" or "nothing" ANY non-"NIL" symbol is considered as "true" in LISP A subtype of everything T Reserved as the default symbol for "true". ALL the data types are subtypes of T. 16

Form A form is an s-expression that is intended to be evaluated. If it is a list, the first element is treated as the operator (functions, macros or special forms) and the subsequent elements are evaluated to obtain the function arguments. Example 1 (+ 2 4) 17

Many Functions S-expression Atom and list Evaluation and its control Form, QUOTE and EVAL Binding variable explicitly SET, SETQ and SETF Cons cell and list CONS, CAR, CDR, NTHCDR, NTH, APPEND, … Predicates TYPEP, SUBTYPEP, EQL, … Conditional constructs IF THEN ELSE, COND, … Iteration LOOP, DO, … Let Function DEFUN Macro DEFMACRO Structure DEFSTRUCT Property list GET 18 Refer to the previous tutorials for details

GUIDED PRACTICE 1. If-then-else 2. Recursion 1 3. Recursion 2 4. Prime Test 5. Tree Traversal 19

1. If-then-else (defun isZero (n) (if (= n 0) (format t "~D is zero.~%" n) (format t "~D is not zero.~%" n))) 20 Try this (isZero 1) (isZero 0)

2. Recursion 1 ;To demonstrate recursion: n (defun sum1 (n) (if (< n 1) 0 (if (= n 1) 1 (+ (sum1 (- n 1)) n)))) 21 Try this (sum 5) (sum 10) (sum -5)

3. Recursion 2 ;To demonstrate recursion - 1^3+2^3+3^3+4^3+...+n^3 (defun sum3 (n) (if (< n 1) 0 (if (= n 1) 1 ) ) ) 22

4. Is Prime ? 23 Try this (isPrime 1) (isPrime 11) (isPrime -5)

5. Tree Traversal ;print the tree in order (defun printTree (tree) (let ((num (car tree)) (ltree (eval (cadr tree))) (rtree (eval (caddr tree)))) (if (not tree) nil (progn (print num) (printTree ltree) (printTree rtree) )) num)) 24 Try this (printTree '(1 nil nil)) (printTree '(2 '(1 nil nil) nil)) (printTree '(2 '(1 nil nil) '(3 nil nil)))

PROGRAMMING EX 1. Define a function 2. Define a recursive function and iterative function 3. Read a segment from a list 4. Remove a segment from a list 5. Sort a list 6. Traverse a tree 7. Use program as data 8. Propose an interesting question for yourself! 25

Programming Exercise 1 Define a function sum(n) which returns the result of …+n 26

Programming Exercise 2 Define a recursive function Fibonacci_r(n) which returns the n th number in the Fibonacci sequence. (Fibonacci_r 1) gives 1 (Fibonacci_r 2) gives 1 (Fibonacci_r 3) gives 2 (Fibonacci_r 4) gives 3 Similarly, define an iterative function Fibonacci_i(n) Finally, execute (mapcar #'Fibonacci_r '( )) 27

Programming Exercise 3 Write a function Extract (L i j) which extracts the i th to j th elements. (Extract '( ) 1 2) gives (2 3) (Extract '( ) 1 1) gives 2 (Extract '( ) 0 4) gives ( ) (Extract '( ) 0 5) gives ( ) (Extract '( ) 5 6) gives NIL 28

Programming Exercise 4 Given a list of length n, we want to remove the cons cells from j to j+1, where j is from 0 to n-1. Define Splice (L i j) for the purpose. (Splice '( ) 1 2) gives (1 4 5) (Splice '( ) 1 1) gives ( ) (Splice '( ) 0 4) gives NIL (Splice '( ) 0 5) gives NIL (Splice '( ) 5 6) gives ( ) 29

Programming Exercise 5 Write a merge sort function for a list of number. 30

Programming Exercise 6 Define a function called tree_all(T), T is a binary tree as described in the tutorial, tree_all will return a list (A1,A2,A3,A4), which A1 is the largest node of T’s left sub tree A2 is the smallest node of T’s left sub tree A3 is the largest node of T’s right sub tree A4 is the smallest node of T’s right sub tree Example >(tree_all '(10 '(38 nil nil) '(20 nil '(11 nil nil)) ( ) >(tree_all '(1 '(10 '(2 nil nil) '(3 nil nil)) '(320 '(24 nil nil) '(95 nil nil) )) ( ) 31 (* (+ (2) (3)) (- (7) (8)))

Programming Exercise 7 Define a macro called run_prog(p x) which returns the value of (p x). Example > (run_prog '(+ x 2) 2) 4 > (run_prog '(+ x (- x 2)) 2) 2 32

Hints 1. Simplify testing.lisp 2. utorial1.html utorial1.html 3. Use do, nthcdr 4. Read next slide Use car, cdr, numberp and define a recursive function 7. Choose among let, lambda, eval, quote and list 33

Hints Hints for Q4 > (setq l '( )) ( ) > (nthcdr 3 l) ( ) > (nthcdr 3 l) ( ) > (setf (cdr (nthcdr 3 l)) (nthcdr 6 l)) ;because clisp doesn't allow setf on nthcdr ( ) > l ( ) 34

Suggested Readings Common Lisp the Language, 2nd Edition Common LISP Hints tutorial.html tutorial.html 35