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

Similar documents
Introduction to Functional Programming and basic Lisp

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

FP Foundations, Scheme

Functions, Conditionals & Predicates

Functional programming in LISP

Functional Programming

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

Review of Functional Programming

Common LISP-Introduction

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

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming. Pure Functional Languages

Functional Languages. Hwansoo Han

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Heap storage. Dynamic allocation and stacks are generally incompatible.

Introduction to Prolog

Functional Programming. Big Picture. Design of Programming Languages

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

CS 314 Principles of Programming Languages

Programming Languages

Functional Programming. Pure Functional Programming

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

Functional Programming. Pure Functional Languages

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

Imperative, OO and Functional Languages A C program is

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

CSC 533: Programming Languages. Spring 2015

Lecture Notes on Lisp A Brief Introduction

LECTURE 16. Functional Programming

Recursive Functions of Symbolic Expressions and Their Application, Part I

Common LISP Tutorial 1 (Basic)

Introduction to lambda calculus Part 3

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

15 Unification and Embedded Languages in Lisp

Introduction to Scheme

Imperative languages

Lisp. Versions of LISP

Recursion & Iteration

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

Lisp History. Lisp and Prolog Background. Lisp Use. Lisp Availability

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scheme Datatypes. expressions. pairs. atoms. lists. symbols. booleans

A Quick Introduction to Common Lisp

Scheme: Expressions & Procedures

Project 2: Scheme Interpreter

Organization of Programming Languages CS3200/5200N. Lecture 11

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

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

User-defined Functions. Conditional Expressions in Scheme

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

Lisp and Prolog Background

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

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

A Brief Introduction to Scheme (II)

A Small Interpreted Language

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

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

Functional programming with Common Lisp

LISP Programming. (23 (this is easy) hello 821)

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

Lambda Calculus see notes on Lambda Calculus

Data abstraction, revisited

CSCI337 Organisation of Programming Languages LISP

1.3. Conditional expressions To express case distinctions like

A LISP Interpreter in ML

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

ALISP interpreter in Awk

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

A little bit of Lisp

Representing Images as Lists of Spots

CS 314 Principles of Programming Languages

CSc 520 Principles of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Introduction to Logic Programming

John McCarthy IBM 704

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

CS 314 Principles of Programming Languages. Lecture 16

Lisp Basic Example Test Questions

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

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

4/19/2018. Chapter 11 :: Functional Languages

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

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

CS A331 Programming Language Concepts

CS 360 Programming Languages Interpreters

Simple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan

Lists in Lisp and Scheme

Functional Programming Lecture 1: Introduction

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

CS 314 Principles of Programming Languages

Lambda Calculus. Gunnar Gotshalks LC-1

Lecture #24: Programming Languages and Programs

CS 61A Discussion 8: Scheme. March 23, 2017

Putting the fun in functional programming

Control in Sequential Languages

Introductory Scheme. Revision 1

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

Transcription:

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 Symbols setq, quote, set List processing functions car, cdr, cons, list, append [ref.: Wilensky, Chap1-2] Overview York University- CSE 3401- V. Movahedi 11_LISP 2

LISP vs. Prolog Prolog Logic Programming Based on Predicate Logic Working with predicates Computation is reasoning, initiated by a query Popular in natural language processing More research on/with Prolog in University of Edinburgh LISP Functional Programming Based on Lambda Calculus Working with functions Computation is evaluation Used in Artificial Intelligence More research on/with LISP in MIT York University- CSE 3401- V. Movahedi 11_LISP 3

LISP Designed by John McCarthy in 1958 in MIT Second-oldest high-level programming language (after Fortran) [Wikipedia] Popular dialects: Common LISP, Scheme We use Common Lisp (www.clisp.org) Execute the command clisp, execute (exit) to exit (or ctrl + D) Theory based on Lambda Calculus by Alonzo Church (1930) -calculus: theory of functions as formulas Easier manipulation of functions using expressions York University- CSE 3401- V. Movahedi 11_LISP 4

LISP LISP: acronym for LISt Processing Joke: LISP is acronym for Lots of Irritating Single Parentheses Primary data structure: Symbolic expression (s-expression): Lists Atoms LISP interpreter: An interactive environment, always evaluating input Example: > (+ 2 3) 5 York University- CSE 3401- V. Movahedi 11_LISP 5

Evaluation (+ 2 3) A list is an s-expression, defined by a pair of parentheses First element is assumed to be a function The rest are arguments to the function Arguments are evaluated as s-expressions themselves Example: > (+ 2 (* 3 4)) 14 LISP evaluation rule: Look at the outermost list first. Evaluate each of its arguments. Use the results as arguments to the outermost function. York University- CSE 3401- V. Movahedi 11_LISP 6

Evaluation (cont.) LISP evaluates everything! Even when the arguments are simple numbers, they are evaluated! Numbers evaluate to themselves > 8 8 A value is returned from the evaluation of an expression Nested Lists > (+ (* 8 9) (- 8 10)) 70 Lots of Irritating Single Parentheses!!! York University- CSE 3401- V. Movahedi 11_LISP 7

Arguments Number of arguments Supply the correct number of arguments > (1+ 5) 6 Otherwise error! It enters debugger, use quit or Ctrl+D to exit debugger + is defined to allow more than 2 arguments >(+ 1 2 3) 6 Supplied arguments vs. actual arguments > (+ 2 (* 3 4)) Supplied args: 2 and (* 3 4) Actual args: 2 and 12 York University- CSE 3401- V. Movahedi 11_LISP 8

Symbols Symbols can serve the role of variables Can be assigned values: > (setq x 8) 8 > x 8 LISP is not case-sensitive! Symbols evaluate to the last value assigned to them Note setq is a special function, First argument is not evaluated Second argument is evaluated and assigned to first argument The value is returned York University- CSE 3401- V. Movahedi 11_LISP 9

Symbols (cont.) Symbols can also serve the role of function identifiers For example +, 1+, setq are all symbols Can have both roles simultaneously! > (setq 1+ 5) 5 > (1+ 7) 8 > 1+ 5 York University- CSE 3401- V. Movahedi 11_LISP 10

S-expressions S-expressions Lists Atoms Numbers Symbols York University- CSE 3401- V. Movahedi 11_LISP 11

Integers: 1, 10,... Ratios: 1/2, 2/3,... > (+ 1/2 1/3) 5/6 More on numbers Floating point numbers: 1.2, 0.25, 3.33E23 Can specify precision by using S, F, D, L for short, single, double, long precision respectively instead of E For example 1.2D10, 2S0 Arithmetic functions on page 429, 434-437 Wilensky York University- CSE 3401- V. Movahedi 11_LISP 12

Lists Use parentheses to denote lists in LISP, no commas e.g. (a b c) >(setq x (a b c)) Error: Undefined function A! Evaluation of lists: first element is assumed to be a function Use quote (short form is ) to prevent evaluation > (setq x (a b c)) (A B C) > (setq x (quote (a b c))) (same as previous) (A B C) York University- CSE 3401- V. Movahedi 11_LISP 13

Set setq is actually set quote > (setq x 5) is same as > (set x 5) Reminder: setq does not evaluate its first argument More examples: > (set x (+ 2 3)) 5 > x 5 > (set x (+ 2 3)) (+ 2 3) > x (+ 2 3) York University- CSE 3401- V. Movahedi 11_LISP 14

Values are S-expressions Assigning a value that is itself a symbol > (setq x y) Y > x Y > (set x (+ 2 3)) 5 > x Y > y 5 Supplied arguments: x and (+ 2 3) Actual arguments: y and 5 York University- CSE 3401- V. Movahedi 11_LISP 15

Lists as binary trees A list is actually a binary tree, consisting of the head and the tail List notation vs. dot notation List notation Dot notation (a) (a. ()) or (a. nil) (a b) (a. (b. nil)) (a b c) (a. (b. (c. nil))) ((a b) c) ((a. (b. nil)). (c. nil)) N/A (a. b) Nil is a constant. Its value can not be changed. Numbers and quoted expressions are also constants. York University- CSE 3401- V. Movahedi 11_LISP 16

Lists as binary trees (a b c) is (a. (b. (c. () ) ) ). a. b. c () York University- CSE 3401- V. Movahedi 11_LISP 17

Heads and Tails car: returns the first element of the list (head) Originating from a CPU instruction: Copy Address Register cdr: returns the list with first element missing (tail) Originating from : Copy Decrement Register Examples: > (car (a b c)) A > (cdr (a b c)) (B C) > (car (cdr (car ((a b)) ) ) ) B York University- CSE 3401- V. Movahedi 11_LISP 18

More predefined functions cadr = car (cdr cadar = car (cdr (car cddaar, cadadr,... Examples: > (cadr (a b c) ) B > (cadar ((a b c)) ) B York University- CSE 3401- V. Movahedi 11_LISP 19

Cons Construct a list using its head and tail second argument must be a list > (cons a (b c) ) (A B C) > (cons (a b) (c d) ) ((A B) C D) Somehow an inverse for car and cdr pair > (setq x (a b c) ) (A B C) > (cons (car x) (cdr x) ) (A B C) Cons is expensive, due to memory allocation and garbage collection York University- CSE 3401- V. Movahedi 11_LISP 20

More list construction functions List: constructs a list of its arguments any number of arguments > (list a b c) (A B C) > (list (1 2) (3 4) ) ((1 2) (3 4)) Append: constructs a list by appending its arguments Any number of arguments Arguments must be lists > (append (a) (b) (c)) (A B C) > (append (1 2) (3 4) ) (1 2 3 4) York University- CSE 3401- V. Movahedi 11_LISP 21

Examples Use car and cdr to return x when applied to (a (b (x d))) (cdr (a (b (x d)))) ((b (x d))) (car (cdr (a (b (x d))) ) ) (b (x d)) (car (cdr (car (cdr (a (b (x d))) )))) (x d) (car (car (cdr (car (cdr (a (b (x d))) ))))) x What is the difference between these expressions? (car (setq x (a b c) ) ) (car (setq x (a b c)) ) A SETQ York University- CSE 3401- V. Movahedi 11_LISP 22