A little bit of Lisp

Similar documents
Lecture Notes on Lisp A Brief Introduction

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

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

Introduction to Lisp

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Robot Programming with Lisp

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

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

Common Lisp. Blake McBride

Common LISP Tutorial 1 (Basic)

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

Common LISP-Introduction

INF4820. Common Lisp: Closures and Macros

Imperative, OO and Functional Languages A C program is

Artificial Intelligence Programming

A Genetic Algorithm Implementation

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

Functions, Conditionals & Predicates

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

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

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

Lisp Basic Example Test Questions

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

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

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

Introduction to Functional Programming and basic Lisp

A Brief Introduction to Common Lisp

(defvar *state* nil "The current state: a list of conditions.")

Functional programming with Common Lisp

Lecture #2 Kenneth W. Flynn RPI CS

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

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

Allegro CL Certification Program

Robot Programming with Lisp

CSCI337 Organisation of Programming Languages LISP

Lisp. Versions of LISP

Object Oriented Programming (OOP)

Allegro CL Certification Program

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

1 CLWEB INTRODUCTION 1

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

Functional Programming with Common Lisp

Announcements. Today s Menu

Associative Database Managment WIlensky Chapter 22

Associative Database Managment

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

Section 10: LISP to Scheme. Evolution of Software Languages

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

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

CS A331 Programming Language Concepts

Midterm Examination (Sample Solutions), Cmput 325

Custom Specializers in Object-Oriented Lisp

CMPUT325 Extensions to Pure Lisp. Extensions to Pure Lisp. Bob Price and Russ Greiner. 5th October 2004

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

Recursion & Iteration

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

Object oriented programming

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

Heap storage. Dynamic allocation and stacks are generally incompatible.

A Quick Introduction to Common Lisp

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

by the evening of Tuesday, Feb 6

CL-STORE: CL Serialization Package

Review of Functional Programming

John McCarthy IBM 704

AllegroCache: an Introduction

Symbolic Computation and Common Lisp

19 Machine Learning in Lisp

MIDTERM EXAMINATION - CS130 - Spring 2005

Functional Programming. Pure Functional Programming

FP Foundations, Scheme

CS 314 Principles of Programming Languages

LISP. Introduction (some words about history...) Theoretical Assumptions Bridging the Gap Overview of Common Lisp Lisp versus ML Conclusion

UMBC CMSC 331 Final Exam

Lisp Users and Vendors Conference. August 10, Tutorial on. Style. Peter Norvig. Sun Microsystems Labs Inc. Kent Pitman. Harlequin, Inc.

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

Documentation for LISP in BASIC

Pattern Matching WIlensky Chapter 21

User-defined Functions. Conditional Expressions in Scheme

GPS: The general problem solver. developed in 1957 by Alan Newel and Herbert Simon. (H. Simon, 1957)

Programming Languages at a Glance

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

Functional Programming. Pure Functional Languages

INDEX. Symbols & Numbers

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

Copyright (c) 1988, by David Michael Betz All Rights Reserved Permission is granted for unrestricted non-commercial use

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

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

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

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

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

XLISP PLUS. Reference Manual. Version 3.0

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Functional Programming

NST: A Unit Test Framework for Common Lisp

Functional programming techniques

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

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

Transcription:

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 recitation (Monday): ask your questions on Lisp/xemacs. Berthe Y. Choueiry (Shu-we-ri) (402)472-5444

B.Y. Choueiry 2 Instructor s notes #3 Features of Lisp 1. Interactive: interpreted and compiled 2. Symbolic 3. Functional 4. Second oldest language but still widely used (Emacs, AutoCad, MacSyma, Yahoo Store, Orbitz, etc.) Software/Hardware We have Allegro Common Lisp (by Franc Inc.): alisp and mlisp There are many old and new dialects (CormanLisp, Kyoto CL, LeLisp, CMU CL, SBCL, ECL, OpenMCL, CLISP, etc.) There have also been Lisp machines (Symbolics, Connection Machine, IT Explorer, others?)

B.Y. Choueiry 3 Instructor s notes #3 Lisp as a functional language (function-name arg1 arg2 etc) 1. Evaluate arguments 2. evaluate function with arguments 3. return the result Functions as arguments to other functions: (name2 (name1 arg1 arg2 etc) arg3 arg2 etc)

B.Y. Choueiry 4 Instructor s notes #3 Symbolic language Atoms: numeric atoms (numbers), symbolic atoms (symbols) Each symbol has: print-name, plist, package, symbol-value, symbol-function Lists: (A B C) A B C (A (B C) D) Symbolic expressions: symbols and lists A B D C NIL NIL NIL

B.Y. Choueiry 5 Instructor s notes #3 More constructs Data types: atoms and lists, packages, strings, structures, vectors, bit-vectors, arrays, streams, hash-tables, classes (CLOS), etc. NIL, T, numbers, strings: special symbols, evaluate to self Basic functions: first (car), rest (cdr), second, tenth setf: does not evaluate first argument cons, append, equal, operations on sets, etc. Basic macros: defun, defmacro, defstruct, defclass, defmethod, defvar, defparameter

B.Y. Choueiry 6 Instructor s notes #3 Special forms: let, let*, flet, labels, progn, Predicates: listp, endp, atom, numberp, symbolp, evenp, oddp, etc. Conditionals: if <test> <then form> <else form>, when <test> <then form>, unless <test> <else form>, cond, case Looping constructs: dolist, dotimes, do, mapcar, loop, Lambda functions

B.Y. Choueiry 7 Instructor s notes #3 A really functional language (defun <function-name> <arg1> <arg2> <arg3>... (flet ((local-function-name <arg a> <arg b>...... <return some value>) ) (... <some-value>)) # (lambda (x)...)..) Regular function Anonymous function Local function defun, flet/labels, lambda

B.Y. Choueiry 8 Instructor s notes #3 What makes Lisp different? Paradigms of AI Programming, Norvig Built-in support for lists Dynamic storage management (garbage collection!) Dynamic typing First-class functions (dynamically created, anonymous) Uniform syntax Interactive environment Extensibility

B.Y. Choueiry 9 Instructor s notes #3 Allegro Common Lisp Free download: www.franz.com/downloads/ Available on SunOS (csce.unl.edu), and Linux. Great integration with emacs Check www.franz.com/emacs/ Check commands distributed by instructor Great development environment Composer: debugger, inspector, time/space profiler, etc. (require composer)

B.Y. Choueiry 10 Instructor s notes #3 ;;; -*- Package: USER; Mode: LISP; Base: 10; Syntax: Common-Lisp -*- (in-package "USER") ;;;; +=========================================================+ ;;;; Source code for the farmer, wolf, goat, cabbage problem ;;;; from Luger s "Artificial Intelligence, 4th Ed." ;;;; In order to execute, run the function CROSS-THE-RIVER ;;;; +=========================================================+

B.Y. Choueiry 11 Instructor s notes #3 ;;; +=============================================+ ;;; State definitions and associated predicates ;;; +=============================================+ (defun make-state (f w g c) (list f w g c)) (defun farmer-side (state) (nth 0 state)) (defun wolf-side (state) (nth 1 state)) (defun goat-side (state) (nth 2 state)) (defun cabbage-side (state) (nth 3 state))

B.Y. Choueiry 12 Instructor s notes #3 ;;; +======================+ ;;; Operator definitions ;;; +======================+ (defun farmer-takes-self (state) (make-state (opposite (farmer-side state)) (wolf-side state) (goat-side state) (cabbage-side state))) (defun farmer-takes-wolf (state) (cond ((equal (farmer-side state) (wolf-side state)) (safe (make-state (opposite (farmer-side state)) (opposite (wolf-side state)) (goat-side state) (cabbage-side state)))) (t nil)))

B.Y. Choueiry 13 Instructor s notes #3 (defun farmer-takes-goat (state) (cond ((equal (farmer-side state) (goat-side state)) (safe (make-state (opposite (farmer-side state)) (wolf-side state) (opposite (goat-side state)) (cabbage-side state)))) (t nil))) (defun farmer-takes-cabbage (state) (cond ((equal (farmer-side state) (cabbage-side state)) (safe (make-state (opposite (farmer-side state)) (wolf-side state) (goat-side state) (opposite (cabbage-side state))))) (t nil)))

B.Y. Choueiry 14 Instructor s notes #3 ;;; +===================+ ;;; Utility functions ;;; +===================+ (defun opposite (side) (cond ((equal side e) w) ((equal side w) e))) (defun safe (state) (cond ((and (equal (goat-side state) (wolf-side state)) (not (equal (farmer-side state) (wolf-side state)))) nil) ((and (equal (goat-side state) (cabbage-side state)) (not (equal (farmer-side state) (goat-side state)))) nil) (t state)))

B.Y. Choueiry 15 Instructor s notes #3 ;;; +========+ ;;; Search ;;; +========+ (defun path (state goal &optional (been-list nil)) (cond ((null state) nil) ((equal state goal) (reverse (cons state been-list))) ((not (member state been-list :test # equal)) (or (path (farmer-takes-self state) goal (cons state been-list)) (path (farmer-takes-wolf state) goal (cons state been-list)) (path (farmer-takes-goat state) goal (cons state been-list)) (path (farmer-takes-cabbage state) goal (cons state been-list)) )))

B.Y. Choueiry 16 Instructor s notes #3 ;;; +==================+ ;;; Canned Execution ;;; +==================+ (defun cross-the-river () (let ((start (make-state e e e e)) (goal (make-state w w w w))) (path start goal)))