Introduction 2 Lisp Part I

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

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

Common LISP Tutorial 1 (Basic)

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

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

Lecture Notes on Lisp A Brief Introduction

Functional Programming. Pure Functional Languages

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Functional Programming. Pure Functional Languages

Artificial Intelligence Programming

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

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

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

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

Introduction to Lisp

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

Imperative, OO and Functional Languages A C program is

Allegro CL Certification Program

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

CS 314 Principles of Programming Languages

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

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Documentation for LISP in BASIC

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

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

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

Introduction to lambda calculus Part 3

Functional Programming

Scheme: Expressions & Procedures

Introduction to Functional Programming and basic Lisp

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

Functional Programming. Pure Functional Programming

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

Functional Programming. Big Picture. Design of Programming Languages

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

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

A Small Interpreted Language

CSCI337 Organisation of Programming Languages LISP

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

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

SOFTWARE ARCHITECTURE 6. LISP

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

Introduction to Artificial Intelligence CISC-481/681 Program 1: Simple Lisp Programs

15 Unification and Embedded Languages in Lisp

by the evening of Tuesday, Feb 6

Project 2: Scheme Interpreter

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

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

FP Foundations, Scheme

Functional programming with Common Lisp

Symbolic Computation and Common Lisp

Introduction 2 Lisp Part III

SCHEME AND CALCULATOR 5b

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Lisp. Versions of LISP

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

Functional programming in LISP

6.001: Structure and Interpretation of Computer Programs

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

The Design and Implementation of a Modern Lisp. Dialect

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Lecture #24: Programming Languages and Programs

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

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

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

Lists in Lisp and Scheme

Common Lisp. Blake McBride

Functional Programming with Common Lisp

Recursive Functions of Symbolic Expressions and Their Application, Part I

Robot Programming with Lisp

INTERPRETATION AND SCHEME LISTS 12

CS 275 Name Final Exam Solutions December 16, 2016

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

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

Programming Languages

INTERPRETATION AND SCHEME LISTS 12

LECTURE 16. Functional Programming

COP4020 Programming Assignment 1 - Spring 2011

A little bit of Lisp

Introduction to Scheme

Lecture #2 Kenneth W. Flynn RPI CS

A Brief Introduction to Scheme (II)

CSc 520 Principles of Programming Languages

INF4820. Common Lisp: Closures and Macros

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

CS A331 Programming Language Concepts

Functional Programming Lecture 1: Introduction

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

CSC 533: Programming Languages. Spring 2015

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

Reasoning About Programs Panagiotis Manolios

Control in Sequential Languages

Scheme as implemented by Racket

Principles of Programming Languages 2017W, Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

A Quick Introduction to Common Lisp

Administrivia. Simple data types

Transcription:

Introduction 2 Lisp Part I Andreas Wichert LEIC-T (Página da cadeira: Fenix) Corpo docente n Andreas (Andrzej) Wichert Praticas n andreas.wichert@tecnico.ulisboa.pt n tel: 214233231 n room: N2 5-7 n http://web.tecnico.ulisboa.pt/andreas.wichert/ 1

Horário de dúvidas n 5º-feira, 14H-17H, 2-N5.7 Guess: What does LISP stand for? n Looney Idiotic Stupid Professor? n Long Incomprehensible String of Parentheses? n LISt Processing? 2

What is LISP? n Originated by John McCarthy (September 4, 1927 October 24, 2011) n In 1959 as an implementation of recursive function theory n http://www-formal.stanford.edu/jmc/ 3

What is LISP? n Conditionals - if-then-else construct n A function type - functions are first-class objects n Recursion n Typed values rather than typed variables n Garbage collection (interpreter) n Programs made entirely of functional expressions that return values Uses Polish notation n The description "Polish" refers to the nationality of logician Jan Łukasiewicz, (21 December 1878 13 February 1956) who invented (prefix) Polish notation in the 1920s n Polish notation, also known as Polish prefix notation or simply prefix notation 4

n (5 6) 7= n Can be written as n ( ( 5 6) 7) n When Polish notation is used as a syntax for mathematical expressions by programming language interpreters, it is readily parsed into abstract syntax trees and can, in fact, define a one-toone representation for the same 5

n ((15 (7 (1 + 1))) 3) (2 + (1 + 1)) = 5 n ((15 (7 (1 + 1))) 3) (2 + (1 + 1)) = 5 n In Lisp n (- (* (/ 15 (- 7 (+ 1 1))) 3) (+ 2 (+ 1 1)))) 5 6

Literature n Practical Common Lisp, Peter Seibel, 2005, Apress, versaõ online n ANSI Common Lisp, Paul Graham, 1995, Prentice Hall, versaõ online n Introduc aõ a Linguagem Lisp, Antońio Leitaõ, 1995, Instituto Superior Tećnico My recommendation. LAND OF LISP, Conrad Barski, 2010, No Starch Press, http://landoflisp.com A very good first contact with the world of Lisp! "Turns out the border between genius and insanity is a pretty cheery place" says Paul Graham! Land of Lisp - The Music Video (Home Work 1, watch the video) 7

n Conrad Barski Conrad Barski, M.D. 8

n Dana Nau n Introduction to Lisp / AI material n https://www.cs.umd.edu/users/nau/ We will use Common lisp J n Purpose of Common Lisp: to unify the main dialects n Thus it contains multiple constructs to do the same things n Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 (R2004) (formerly X3.226-1994 (R1999)). 9

Installing Common Lisp n CLISP n Download a CLISP installer from n http://clisp.cons.org/. Running clisp 10

Clozure Common Lisp n http://ccl.clozure.com n Like all Common Lisp environments, CLISP will automatically place you into a read-eval-print loop (REPL) n The code on the previous slide runs interpretively A few Common Lisps will compile the code every time you load it n Compiling makes your programs run faster, and may detect some errors 11

Evaluating >1 1 >pi 3.1415926535897932385L0 > t T > nil NIL 12

Symbols n Common Lisp symbols are typically made up of letters, numbers, and characters like + - / * = < >?! _ n ice9, my-killer-app27, and even -- <<==>>--. 13

Numbers > (+ 1 1.0) 2.0 > (expt 53 53) 2435684816502271213247760652010472551853345312 86856408445051308795767206091502 23301256150373 > (/ 4 6) 2/3 > (/ 4.0 6) 0.6666667 > (- 5 6 7) -8 Why? Evaluates from left >(- (- 5 6) 7) -8 14

Strings (princ "Tutti Frutti") Tutti Frutti "Tutti Frutti" n The text appears twice n First, we see the actual printing caused by the princ command. n REPL will always show the result of evaluating the entered expression the princ function returns a value, the source string. > (princ "He yelled \"Stop that thief!\" from the busy street.") He yelled "Stop that thief!" from the busy street. "He yelled \"Stop that thief!\" from the busy street." 15

Code Mode n > (expt 2 3) n 8 n This calculates 2^3 = 8 n It does this by calling expt, which computes an exponent. 16

Data Mode > '(expt 2 3) (EXPT 2 3) The single quote tells Lisp to treat the subsequent form as a chunk of data simply a list of items. > (quote (expt 2 3)) (EXPT 2 3) > '(1 2 3) (1 2 3) > ''(1 2 3) '(1 2 3) 17

But (Why? Read the error message) > ( 1 2 3) *** - EVAL: 1 is not a function name; try using a symbol instead The following restarts are available: USE-VALUE :R1 Input a value to be used instead. ABORT :R2 Abort debug loop ABORT :R3 Abort debug loop ABORT :R4 Abort debug loop ABORT :R5 Abort main loop Predicates 18

Numerical predicates (> 5 4 3) T (> 5 4 6) NIL (<= 3 2) NIL (= 1 2) NIL (= 1 1 1) T >(zerop 0) T >(integerp 5) T n > (eq 1234567890 1234567890)??? 19

n The eq function is the simplest of all the Lisp comparison functions, and it s also very fast. n It doesn t really work for comparing items besides symbols n The eql command is similar to the eq command, but unlike eq, it also handles comparisons of numbers and characters n The equalp command is essentially the same as the equal command, except that it can handle some difficult comparison cases with a bit of extra sophisti- cation 20

Atoms n Every Lisp object is either an atom or a list Lists in Lisp n A cons cell looks like this: n We create the list '(1 2 3) 21

Lists Dot notation 22

The cons Function link any two pieces of data >(cons 'chicken 'cat) (CHICKEN. CAT) > (cons 'chicken 'nil) (CHICKEN) > (cons 'chicken ()) (CHICKEN) > (cons 'beef (cons 'chicken nil)) (BEEF CHICKEN) > (cons 'beef (cons 'chicken nil)) (BEEF CHICKEN) >(cons 'pork '(beef)) (PORK BEEF) > (cons 'pork '(beef chicken)) (PORK BEEF CHICKEN) 23

The car and cdr Functions > (car '(pork beef chicken)) PORK > (cdr '(pork beef chicken)) (BEEF CHICKEN) > (cdr '(pork beef chicken)) (BEEF CHICKEN) > (car '(beef chicken)) BEEF > (car (cdr '(pork beef chicken))) BEEF > (cadr '(pork beef chicken)) BEEF 24

Nested Lists n Lists can contain other lists. '(cat (duck bat) ant) >(car '((peas carrots tomatoes) (pork beef chicken))) (PEAS CARROTS TOMATOES) 25

if > (if (= (+ 1 2) 3) 'yup 'nope) YUP >(if (= (+ 1 2) 4) 'yup 'nope) NOPE >(if '(1) 'the-list-has-stuff-in-it 'the-list-is-empty) THE-LIST-HAS-STUFF-IN-IT >(if '() 'the-list-has-stuff-in-it 'the-list-is-empty) THE-LIST-IS-EMPTY 26

> (if (oddp 5) 'odd-number 'even-number) ODD-NUMBER > (if (oddp 5) 'odd-number (/ 1 0)) ODD-NUMBER Why? Special form, executes only the true branch 27

Defining Global Variables 28

Local Variables Editor 29

To load / compile n (load <nome-ficheiro>) n (load projecto.lisp ) n (compile-file "projecto") n (load "projecto") 30

31