Introduction 2 Lisp Part III

Size: px
Start display at page:

Download "Introduction 2 Lisp Part III"

Transcription

1 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

2 (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 '( )) ( ) > (rest list1) (B C) [9]> (rest list2) (2 3 4) [10]> (first list1) A [11]> (null list1) NIL [12]> (null '()) T 2

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

4 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

5 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

6 (II) Data Types and Generic Programming 6

7 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

8 &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 '( ))) #1A(( )) > (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

9 Common Lisp Structures 9

10 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

11 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

12 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

13 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

14 n Fundamental programming-like algebra developed back in the 1930s by Alonzo Church ( ) 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

15 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)

16 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

17 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

18 Example 18

19 [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

20 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

21 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

22 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

23 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 24

25 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

26 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

27 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

28 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

29 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

30 (V) More about Common Lisp an AI n The Minimax Algorithm n Chapter 15 in LoL 30

A Small Interpreted Language

A Small Interpreted Language A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

More information

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

From the λ-calculus to Functional Programming Drew McDermott Posted

From the λ-calculus to Functional Programming Drew McDermott Posted From the λ-calculus to Functional Programming Drew McDermott drew.mcdermott@yale.edu 2015-09-28 Posted 2015-10-24 The λ-calculus was intended from its inception as a model of computation. It was used by

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 02: Bare Bones Haskell Syntax: Data == Abstract Syntax Trees Functions == Rewrite Rules on ASTs

More information

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

More information

Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

More information

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

4/19/2018. Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

More information

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Programming with Math and Logic

Programming with Math and Logic .. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

More information

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

More information

6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs 6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

More information

Introduction to Functional Programming

Introduction to Functional Programming PART I TE RI AL Introduction to Functional Programming MA CHAPTER 1: A Look at Functional Programming History CO PY RI GH TE D CHAPTER 2: Putting Functional Programming into a Modern Context 1A Look at

More information

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

More information

Programming Language Pragmatics

Programming Language Pragmatics Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen

More information

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

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

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam Writing code that I'm not smart enough to write A funny thing happened at Lambda Jam Background "Let s make a lambda calculator" Rúnar Bjarnason Task: write an interpreter for the lambda calculus Lambda

More information

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

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get

More information

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

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp. Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

More information

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

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Common LISP-Introduction

Common LISP-Introduction Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally

More information

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

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires 1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

More information

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

ACT-R RPC Interface Documentation. Working Draft Dan Bothell AC-R RPC Interface Documentation Working Draft Dan Bothell Introduction his document contains information about a new feature available with the AC-R 7.6 + software. here is now a built-in RPC (remote

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

More information

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

More information

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

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley PAIRS AND LISTS 6 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 29, 2010 1 Pairs 1.1 Overview To represent data types

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Functional Programming with Common Lisp

Functional Programming with Common Lisp Functional Programming with Common Lisp Kamen Tomov ktomov@hotmail.com July 03, 2015 Table of Contents What is This About? Functional Programming Specifics Is Lisp a Functional Language? Lisp Says Hi Functional

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

More information

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

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

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer

More information

A Brief Introduction to Common Lisp

A Brief Introduction to Common Lisp A Brief Introduction to Common Lisp David Gu Schloer Consulting Group david_guru@gty.org.in A Brief History Originally specified in 1958, Lisp is the second-oldest highlevel programming language in widespread

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)

More information

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

More information

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017 Agenda 2 Previously Common

More information

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

Debugging in LISP. trace causes a trace to be printed for a function when it is called trace causes a trace to be printed for a function when it is called ;;; a function that works like reverse (defun rev (list) (cons (first (last list)) (rev (butlast list)))) USER: (trace rev) ; note trace

More information

Introduction. chapter Functions

Introduction. chapter Functions chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

More information

Repetition Through Recursion

Repetition Through Recursion Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

More information

CS115 - Module 9 - filter, map, and friends

CS115 - Module 9 - filter, map, and friends Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Intermezzo 3 (Section 18); Sections 19-23. Abstraction abstraction, n. 3a.... The process of isolating properties or

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

Functional Programming

Functional Programming Functional Programming Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. Brief

More information

INF4820. Common Lisp: Closures and Macros

INF4820. Common Lisp: Closures and Macros INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

More information

A little bit of Lisp

A little bit of Lisp 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

More information

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Introduction 2 Lisp Part I

Introduction 2 Lisp Part I 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/

More information

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012 SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

More information

CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm. Question Max Points Total 100 CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

More information

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

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work History Functional Programming Based on Prof. Gotshalks notes on functionals 1977 Turing 1 Lecture John Backus described functional programming The problem with current languages is that they are word-at-a-time

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Functions. Nate Foster Spring 2018

Functions. Nate Foster Spring 2018 Functions Nate Foster Spring 2018 A0: Warmup Worth only 1% of final grade; other assignments will be 5% much easier coding problems intended to give you low-stakes experience with 3110 workflow Please

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

CS1102: Adding Error Checking to Macros

CS1102: Adding Error Checking to Macros CS1102: Adding Error Checking to Macros Kathi Fisler, WPI October 8, 2004 1 Typos in State Machines The point of creating macros for state machines is to hide language details from the programmer. Ideally,

More information

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

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples: COMP80 Programming Languages Slides Courtesy of Prof. Sam Guyer Lambda Calculus Formal system with three parts Notation for functions Proof system for equations Calculation rules called reduction Idea:

More information

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.

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. 1 of 10 23/07/2016 05:15 Paul's Online Math Notes Calculus III (Notes) / Line Integrals / Line Integrals - Part I Problems] [Notes] [Practice Problems] [Assignment Calculus III - Notes Line Integrals Part

More information

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

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

More information

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

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018 CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018 Taken from assignments by Profs. Carl Offner and Ethan Bolker Part 1 - Modifying The Metacircular Evaluator

More information

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

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

More information

[2:3] Linked Lists, Stacks, Queues

[2:3] Linked Lists, Stacks, Queues [2:3] Linked Lists, Stacks, Queues Helpful Knowledge CS308 Abstract data structures vs concrete data types CS250 Memory management (stack) Pointers CS230 Modular Arithmetic !!!!! There s a lot of slides,

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Notes on Higher Order Programming in Scheme. by Alexander Stepanov by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

More information

Lecture Notes on Lisp A Brief Introduction

Lecture Notes on Lisp A Brief Introduction Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham

More information

The Design and Implementation of a Modern Lisp. Dialect

The Design and Implementation of a Modern Lisp. Dialect The Design and Implementation of a Modern Lisp Dialect Sam Davis Nicholas Alexander January 26, 2006 Abstract Lisp, invented in 1958 by John McCarthy, revolutionized how programs could be written and expressed.

More information

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus see notes on Lambda Calculus Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols,

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

More information

Lecture #5 Kenneth W. Flynn RPI CS

Lecture #5 Kenneth W. Flynn RPI CS Outline Programming in Lisp Lecture #5 Kenneth W. Flynn RPI CS We've seen symbols in three contexts so far: > (setf sym ) (let ((sym ))...) >'Sym SYM -- Context The first of these refers to a special (or

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

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

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic. Coding Workshop Learning to Program with an Arduino Lecture Notes Table of Contents Programming ntroduction Values Assignment Arithmetic Control Tests f Blocks For Blocks Functions Arduino Main Functions

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

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

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Java We will be programming in Java in this course. Partly because it is a reasonable language, and partly because you

More information

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

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM .consulting.solutions.partnership Clojure by Example A practical introduction to Clojure on the JVM Clojure By Example 1 Functional Progamming Concepts 3 2 Clojure Basics 4 3 Clojure Examples 5 4 References

More information

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

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 7 Lambda Calculus Dan Grossman Spring 2011 Where we are Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now:

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

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

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions Whereweare CS-XXX: Graduate Programming Languages Lecture 7 Lambda Calculus Done: Syntax, semantics, and equivalence For a language with little more than loops and global variables Now: Didn t IMP leave

More information

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

PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects, MITOCW Lecture 5B PROFESSOR: Well, now that we've given you some power to make independent local state and to model objects, I thought we'd do a bit of programming of a very complicated kind, just to illustrate

More information

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1 Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)

More information

Dynamic Arrays and Amortized Analysis

Dynamic Arrays and Amortized Analysis Department of Computer Science and Engineering Chinese University of Hong Kong As mentioned earlier, one drawback of arrays is that their lengths are fixed. This makes it difficult when you want to use

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

Introduction to Scheme

Introduction to Scheme How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

More information

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

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

More information

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

So far, we have seen two different approaches for using computing to solve problems: Chapter 10 Objects By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would

More information

Checked and Unchecked Exceptions in Java

Checked and Unchecked Exceptions in Java Checked and Unchecked Exceptions in Java Introduction In this article from my free Java 8 course, I will introduce you to Checked and Unchecked Exceptions in Java. Handling exceptions is the process by

More information

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1 RACKET BASICS, ORDER OF EVALUATION, RECURSION 1 COMPUTER SCIENCE 61AS 1. What is functional programming? Give an example of a function below: Functional Programming In functional programming, you do not

More information