Basics of Using Lisp! Gunnar Gotshalks! BLU-1

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

Lisp Basic Example Test Questions

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

Functions, Conditionals & Predicates

INF4820. Common Lisp: Closures and Macros

Common LISP-Introduction

FP Foundations, Scheme

Lecture Notes on Lisp A Brief Introduction

Environments

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Introduction to Functional Programming and basic Lisp

CSCI337 Organisation of Programming Languages LISP

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

Lambda Calculus. Gunnar Gotshalks LC-1

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Vanilla Lisp Shell (VLS)

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

Evaluating Scheme Expressions

Announcements. Today s Menu

Lambda Calculus LC-1

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

Allegro CL Certification Program

*.lsp Commands: + - * / setq setvar getvar getpoint defun command getstring appload vlide

Common LISP Tutorial 1 (Basic)

Lambda Calculus see notes on Lambda Calculus

Functional programming with Common Lisp

Reflection for the Masses

Type Bindings. Static Type Binding

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

2.4 Structuring programs

entity tomove over backward forward character C-b C-f word M-b M-f line C-p C-n go to line beginning (or end) C-a C-e sentence M-a M-e paragraph M- M-

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

a little more on macros sort of like functions, but..

Compiler construction

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

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

Functional Programming. Pure Functional Programming

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Functional Programming. Pure Functional Languages

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Common Lisp Object System Specification. 1. Programmer Interface Concepts

Principles of Programming Languages 2017W, Functional Programming

Allegro CL Certification Program

CMSC 330: Organization of Programming Languages

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

PL Revision overview

Fall 2017 Programming Project #5 Databases CMSC 420 Hanan Samet

Names, Scope, and Bindings

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Functional Programming. Pure Functional Languages

Robot Programming with Lisp

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

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

CS 360 Programming Languages Interpreters

Functional Programming

luacheck Documentation

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

XLISP PLUS. Reference Manual. Version 3.0

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Section 10: LISP to Scheme. Evolution of Software Languages

Specification and verification of a simple machine

Control Flow February 9, Lecture 7

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Functional programming techniques

LFE - a lisp on the Erlang VM

Functional Programming. Big Picture. Design of Programming Languages

Lisp as a Second Language Chapter 1 Functional Style

Macroexpand-All: An Example of a Simple Lisp Code Walker

UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

Names, Scope, and Bindings

ALISP interpreter in Awk

Review of Functional Programming

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

CS61A Notes Week 13: Interpreters

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

Extending Pattern Directed Inference Systems. EECS 344 Winter 2008

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

An Explicit Continuation Evaluator for Scheme

B l o c k B i n d i n g s

An Introduction to Functions

Lecture 12: Conditional Expressions and Local Binding

Magit-Popup User Manual

CSE450. Translation of Programming Languages. Lecture 10: Flow Control and Lexical Scope

The role of semantic analysis in a compiler

Names, Scope, and Bindings

Operational Semantics of Cool

CS 314 Principles of Programming Languages

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Assignment 7: functions and closure conversion (part 1)

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

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

This should prevent residual sexism, racism, favoritism lurking in the unconscious of your professor & TA from biasing grading!!

Transcription:

Basics of Using Lisp BLU-1

Entering Do Lisp work Exiting Getting into and out of Clisp % clisp (bye)» A function with no arguments» CTRL d can also be used Documentation files are in the directory» /cse/local/doc/clisp BLU-2

Do Lisp work Edit in files with extension.lsp Load the files ( load 'filename.lsp ) > Loading executes the S-expressions in the file > Loading defines symbols > No other computational effects are seen > Comments begin with ; Interactively execute S-expressions» Invoke functions» Use setq to define symbol values» Use defun to define functions BLU-3

The Lisp Interpreter Is a loop over the following functions» Read > an S-expression» Eval > the S-expression» Print > the result You can redefine these functions» But it is dangerous if you do not know what you are doing. BLU-4

Free variable > global variable defined at the upper level (outside of function definitions)» Within a function consistently changing the name of a free variable will normally alter the semantics of the function Free and bound variables (defun x ( a ) ( + a y ) ) is NOT the same as (defun x ( a ) ( + a z ) ) BLU-5

Bound variable Bound variables > local variable defined in the parameter list of a function» Within a function consistently changing the name of a bound variable will not alter the semantics of the function (defun x ( a y ) ( + a y ) ) is the same as (defun x ( a z ) ( + a z ) ) BLU-6

Static & Dynamic scoping Consider the following ( defun f1 ( v1 ) ( f2 v1 ) ) ;; v1 defined argument ( defun f2 ( v2 ) ( 1+ v1 ) ) ;; is v1 defined? ( f1 7 ) Under static (lexical) scoping invoking ( f1 7 ) produces an error as v1 is undefined in f2 Under dynamic scoping v1 in f2 is defined because f2 is executed in the environment of f1 in which v1 is defined» Dynamic scoping leads to the funarg problem as function arguments can shadow (hide) global variables BLU-7

Execution Environment An environment consists of binding between a set of symbols and their values ( ( A 1 ) ( B 5 )... ( D ( a b c ) ) ) At the interpreter level global symbols are created, using setq or defun, giving a global environment The value of a symbol is looked up in the environment Evaluating a function causes the parameters to be prepended to the appropriate environment» Evaluating (f1 3) defined as (defun f1 (v1) ( f2 v1 ))» creates the environment ( ( v1 3 ) ( A 1 ) ( B 5 )... ( D ( a b c ) ) ) BLU-8

Execution Environment 2 We evaluate (f2 v1) in the context ( ( v1 3 ) ( A 1 ) ( B 5 )... ( D ( a b c ) ) ) v1 has the value 3 passed as an argument to f2 f2 is defined as ( defun f2 ( v2 ) ( 1+ v1 ) ) What environment does f2 use?» We have two choices > Dynamic scoping > Static scoping BLU-9

Execution Environment 3 Dynamic scoping» passes the existing environment ( ( v1 3 ) ( A 1 ) ( B 5 )... ( D ( a b c ) ) )» after prepending ( v2 3 ) v2 is the parameter of f2 and 3 is the argment from f1 > The following is passed ( (v2 3 ) ( v1 3 ) ( A 1 ) ( B 5 )... ( D ( a b c ) ) ) > So v1 has a definition in the environment» The environment grows and shrinks on entry and exit from functions > A different environment for every function BLU-10

Execution Environment 4 Static scoping» passes the environment in the context of the definition of f2» the same environment passed to f1» after prepending ( v2 3 ) > The following environment is passed ( (v2 3 ) ( A 1 ) ( B 5 )... ( D ( a b c ) ) ) > So v1 has NO definition in the environment» Environment on entry is fixed by the static structure > The same environment for every function BLU-11

Dynamic scoping funarg problem Dynamic scoping leads to the funarg problem as function arguments can shadow global variables» ( defun funarg ( func arg ) ( funcall func arg ) )» ( defun timesarg (x ) ( * arg x ) )» ( setq arg 2 )» ( funarg timesarg 3 ) In a static environment the result is 6 In a dynamic environment the result is 9 Is Clisp dynamically or statically scoped? BLU-12

Do Lisp Work... Reminder Do not use setq within function definitions Setq creates global symbols NOT local symbols Very poor programming practice BLU-13