Introduction 2 Lisp Part III

Similar documents
A Small Interpreted Language

Functional Programming. Pure Functional Languages

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

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

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

From the λ-calculus to Functional Programming Drew McDermott Posted

CS 314 Principles of Programming Languages

Documentation for LISP in BASIC

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

Functional Programming. Big Picture. Design of Programming Languages

Functional Languages. Hwansoo Han

Functional Programming. Pure Functional Languages

CS 320: Concepts of Programming Languages

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

Chapter 11 :: Functional Languages

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

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

CSCC24 Functional Programming Scheme Part 2

Programming with Math and Logic

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

6.001: Structure and Interpretation of Computer Programs

Introduction to Functional Programming

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

Programming Language Pragmatics

SCHEME AND CALCULATOR 5b

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

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.

1.3. Conditional expressions To express case distinctions like

Common LISP-Introduction

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

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

CS558 Programming Languages

CS558 Programming Languages

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Functional Programming. Pure Functional Programming

6.001 Notes: Section 8.1

Functional Programming

Functional Programming Languages (FPL)

CS 360 Programming Languages Interpreters

Functional Programming with Common Lisp

CS 314 Principles of Programming Languages

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

A Brief Introduction to Common Lisp

Building a system for symbolic differentiation

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

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

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

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

Introduction. chapter Functions

Repetition Through Recursion

CS115 - Module 9 - filter, map, and friends

Functional programming with Common Lisp

Functional Programming

INF4820. Common Lisp: Closures and Macros

A little bit of Lisp

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

Introduction 2 Lisp Part I

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

CSE413 Midterm. Question Max Points Total 100

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

Chapter 4 Defining Classes I

Functions. Nate Foster Spring 2018

Chapter 15. Functional Programming Languages

CS1102: Adding Error Checking to Macros

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

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

[2:3] Linked Lists, Stacks, Queues

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Lecture Notes on Lisp A Brief Introduction

The Design and Implementation of a Modern Lisp. Dialect

Lambda Calculus see notes on Lambda Calculus

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Lecture #5 Kenneth W. Flynn RPI CS

Lecture 3: Recursion; Structural Induction

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

User-defined Functions. Conditional Expressions in Scheme

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

LECTURE 16. Functional Programming

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects,

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Dynamic Arrays and Amortized Analysis

More Lambda Calculus and Intro to Type Systems

Functional Programming

Introduction to Scheme

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

So far, we have seen two different approaches for using computing to solve problems:

Checked and Unchecked Exceptions in Java

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Transcription:

Introduction 2 Lisp Part III Andreas Wichert LEIC-T (Página da cadeira: Fenix) (I) Home Work (II) Data Types and Generic Programming (III) Lisp is Science (IV) Lisp Macros (V) More about Common Lisp an AI 1

(I) Home Work n (1) Construir uma nova lista que corresponde a juntar duas listas [2]> (setf list1 '(a b c)) (A B C) [3]> list1 (A B C) [4]> (setf list2 '(1 2 3 4)) (1 2 3 4) > (rest list1) (B C) [9]> (rest list2) (2 3 4) [10]> (first list1) A [11]> (null list1) NIL [12]> (null '()) T 2

n (junta list1 list2) n (A B C 1 2 3 4) [6]> (trace junta) ;; Tracing function JUNTA. (JUNTA) [7]> (junta list1 list2) 1. Trace: (JUNTA '(A B C) '(1 2 3 4)) 2. Trace: (JUNTA '(B C) '(1 2 3 4)) 3. Trace: (JUNTA '(C) '(1 2 3 4)) 4. Trace: (JUNTA 'NIL '(1 2 3 4)) 4. Trace: JUNTA ==> (1 2 3 4) 3. Trace: JUNTA ==> (C 1 2 3 4) 2. Trace: JUNTA ==> (B C 1 2 3 4) 1. Trace: JUNTA ==> (A B C 1 2 3 4) (A B C 1 2 3 4) n (2) Construir uma func aõ que inverte uma lista usando processo recursivo 3

n Case 1: list1 is nil. The reversal of list1 is simply nil. The result of appending A to the end of an empty list is simply help itself. 4

n Case 2: list1 is constructed by cons. n Now list1 is composed of two parts: (first list1) and (rest list1). n Observe that (first list1) is the last element in the reversal of list1. n If we are to append help to the end of the reversal of list1, then (first list1) will come immediately before the elements of help n Observing the above, we recognize that we obtain the desired result by recursively appending (cons (first list1) help) to the reversal of (rest list1). 5

(II) Data Types and Generic Programming 6

Optional n Common Lisp allows functions to be defined as having optional arguments by placing the keyword &optional before the lambda variables that are to be bound to the optional arguments n The optional arguments are missing in a particular form, the lambda variables are bound to nil Optional 7

&key n You may define your own functions to have keyword parameters by including &key n Keyword arguments that are not supplied default to nil Arrays with several Dimensions (setf exemplo2 (make-array '(2 3) :initial-element :b)) #2A((:B :B :B) (:B :B :B)) > (setf exemplo3 (make-array '(4) :element-type 'bit :initial-contents '(0 1 0 1))) #1A((0 1 0 1)) > (setf exemplo4 #2A((1 2 3) (4 5 6))) #2A((1 2 3) (4 5 6)) ;Selectores: aref, array-dimensions, array-dimension > (aref exemplo4 1 2) 6 > (array-dimensions exemplo4) (2 3) 8

Common Lisp Structures 9

Reduce n One particularly useful generic sequence function is reduce n The reduce function allows you to iterate through a sequence and distill it down into a single result. Generic n Great benefit of the reduce function is that it is generic, as is true for all these sequence functions n It can reduce lists, arrays, or strings in exactly the same way, 10

n Find the largest even number in the list n We are using lambda n The first argument is the largest even number we ve found so far n The second argument is the next number from the list n If the latest number is better than the previous best, we return it n Otherwise, we return the previous best n We pass an explicit initial value to the reduce function by passing in a keyword parameter named :initial-value 11

n Great benefit of the reduce function is that it is generic, as is true for all these sequence functions n It can reduce lists, arrays, or strings in exactly the same way, (II) Lisp is Science 12

n Lisp s main claim to fame is as an academic tool, appropriate for tackling the most complicated scientific problems n Functional programming is a style of programming where we write all of our code using functions. 13

n Fundamental programming-like algebra developed back in the 1930s by Alonzo Church (1903 1995) n In the lambda calculus, you run a program by performing substitution rules on the starting program to determine the result of a function. n The purpose of the function is to do nothing other than to return a result. Functional Style (I) n The function always returns the same result, as long as the same arguments are passed into it. (This is often referred to as referential transparency.) n The function never references variables that are defined outside the function, unless we are certain that these variables will remain constant 14

Functional Style (II) n No variables are modified (or mutated, as functional programmers like to say) by the function n The purpose of the function is to do nothing other than to return a result n The function doesn t do anything that is visible to the outside world, such as pop up a dialog box on the screen Functional Style (III) n The function doesn t take information from an outside source, such as the keyboard or the hard drive n Example: n > (sin 0.5) 0.47942555 15

Side Effect n Whenever a piece of code does something that is visible to the outside world, such as display a dialog box on the screen, we say that the code causes a side effect. n Functional programmers think of such side effects as making your code dirty. Imperative Code n The technical term for such dirty code that contains side effects is imperative code. n The term imperative implies that the code is written in a cookbook style, where you basically say things like first do this, and then do that. n Imperative code is the opposite of functional code 16

Two Parts n You should break your program into two parts n The first, and biggest part, should be completely functional and free of side effects. This is the clean part of your program. n The second, smaller part of your program is the part that has all the side effects, interacting with the the outside world. This code is dirty and should be kept as small as possible. n If a piece of code pops up a dialog box, for example, we deem it dirty and banish it to the imperative section of our code n Things like dialog boxes are not really math, and we shouldn t let them play with our math functions and other clean, functional code 17

Example 18

[4]> (main-loop) Please enther the name of a new widget:apple The database contains the folowing: (APPLE) Please enther the name of a new widget:alfa-romeo The database contains the folowing: (ALFA-ROMEO APPLE) Please enther the name of a new widget:vespa The database contains the folowing: (VESPA ALFA- ROMEO APPLE) Please enther the name of a new widget: Code Composition n Programming should make it easy for you to take different pieces of code and use them together to solve a task n Higher-order programming, which lets you use functions that accept other functions as parameters 19

n Code composition can be a challenge to a beginning functional programmer n Suppose we want to add two to every number in the following list n To do this, we will need to write code to traverse the list, as well as write code to add two to a number n One possible nai ve (and imperative) way 20

n Code structured like this is potentially very efficient n It destroys the original list n We needed to create a variable n Functional Style n There is no longer a clear delineation between the code that adds two to items in the list and the code that traverses the list 21

Higher-Order Programming (IV) Lisp Macros n Suppose, for example, that your program needs a special add function n Why do you need so many parentheses to declare your variable x? 22

n However, you can t just write a regular function to hide those parentheses, because the let command can do things a regular Lisp function can t support n The let command is a special form. n Macros let us get rid of the superfluous parentheses. Let s create a new macro named let1: n Like a function, it has a name (in this case, let1) and arguments passed to it 23

24

This magic wand is called macro expansion Macro Expansion n Macro Expansion, a special transformation that your code is put through before the core of the Lisp interpreter/ compiler gets to see it. n The job of the macro expander is to find any macros in your code (such as our let1 macro) and to convert them into regular Lisp code. 25

n A macro, runs before the program does, when the program is read and compiled by your Lisp environment. n This is called macro expansion time. n When we define a new macro with the defmacro command, we re basically teaching the Lisp macro expansion system a new transformation that it can use to translate code before running a program. n The first line of this defmacro tells the macro expander, n Hey, if you see a form in code that begins with let1, here s what you need to do to transform it into standard Lisp. 26

n The let1 macro has three such arguments passed into it: var, val, and body n Hey, if you see a form in code that begins with let1, here s what you need to do to transform it into standard Lisp. n var The first argument is the name of the variable we re defining. n val The second expression holds the code that determines the value of the variable n body The third expression inside a let1 call is the body code, which makes use of the new variable that s created 27

n The let command is allowed to have multiple statements in its body n This is why, in the defmacro command defining let1, the final body argument has the special keyword &body in front of it. n This tells the macro expander Give me all remaining expressions in the macro in a list. 28

Dangers n Macros allow us to write code that generates other code n They let you trick the Lisp compiler/ interpreter into accepting your own customized n The main drawback of macros is that they can make it hard for other programmers to understand your code 29

(V) More about Common Lisp an AI n The Minimax Algorithm n Chapter 15 in LoL http://aima.cs.berkeley.edu 30