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

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

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

Reasoning About Programs Panagiotis Manolios

Functional Programming. Pure Functional Languages

Lecture Notes on Lisp A Brief Introduction

Common Lisp. Blake McBride

CSCI337 Organisation of Programming Languages LISP

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

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Functional Programming. Pure Functional Languages

Lisp Basic Example Test Questions

A little bit of Lisp

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

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

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

Common LISP Tutorial 1 (Basic)

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

Lisp. Versions of LISP

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

Introduction to Lisp

Common LISP-Introduction

Artificial Intelligence Programming

Allegro CL Certification Program

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

Imperative, OO and Functional Languages A C program is

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

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

Symbolic Computation and Common Lisp

Robot Programming with Lisp

CS 314 Principles of Programming Languages

Lecture #2 Kenneth W. Flynn RPI CS

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

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

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

Reasoning About Programs Panagiotis Manolios

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

A Quick Introduction to Common Lisp

Functions, Conditionals & Predicates

Lecture #24: Programming Languages and Programs

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

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

A Genetic Algorithm Implementation

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Project 1: Scheme Pretty-Printer

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

Functional Programming Lecture 1: Introduction

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

Functional Programming. Pure Functional Programming

An Introduction to Emacs Lisp

CSC 533: Programming Languages. Spring 2015

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

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 Verification with ACL2

Documentation for LISP in BASIC

Functional Programming. Big Picture. Design of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Verifying Centaur s Floating Point Adder

by the evening of Tuesday, Feb 6

INTERPRETERS 8. 1 Calculator COMPUTER SCIENCE 61A. November 3, 2016

Functional programming with Common Lisp

SCHEME AND CALCULATOR 5b

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

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

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

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

Concepts of programming languages

CSc 520 Principles of Programming Languages

Introduction 2 Lisp Part I

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

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


CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

An Explicit Continuation Evaluator for Scheme

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

CS 314 Principles of Programming Languages. Lecture 16

Scheme: Expressions & Procedures

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

Review of Functional Programming

Warm-up and Memoization

PROBLEM SOLVING 11. July 24, 2012

University of Maine School of Computing and Information Science COS 470/570

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

Recursion & Iteration

Project 2: Scheme Interpreter

These are notes for the third lecture; if statements and loops.

CSCC24 Functional Programming Scheme Part 2

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Functional Programming

Civil Engineering Computation

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Imperative languages

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

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

Vanilla Lisp Shell (VLS)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

ALISP interpreter in Awk

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

Transcription:

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial About this Document This document was written to accompany an in-person Lisp tutorial. Therefore, the information on this document alone is not likely to be sufficient to get a good understanding of Lisp. However, the functions and operators that are listed here are a good starting point, so you may look up specifications and examples of usage for those functions in the Common Lisp HyperSpec (CLHS) to get an understanding of their usage. Getting Started Note on Common Lisp Implementations I will use Allegro Common Lisp since it is what is used by Len (and my research). However, for the purposes of this class we won t be using many (if any) features that are specific to common lisp implementations so you may use any of the common lisp implementations available in the URCS servers. If you are using any libraries that are implementation-specific for assignments in this class, I would strongly urge you to rethink your approach if possible. If we (the TAs) deem any of these features to be solving too much of the assigned problem, you will not receive full credit for the assignment. To see if a function is in the Common Lisp specifications see the Common Lisp HyperSpec (CLHS). Simply google the function followed by clhs to see if an entry shows up. Available Common Lisp implementations (as far as I know) : - Allegro Common Lisp ( acl or alisp will start an Allegro REPL) - CMU Common Lisp ( cmucl or lisp will start a CMUCL REPL) - Steel Bank Common Lisp ( sbcl will start an SBCL REPL) IDE/Editor It is not important for this class to have an extensive IDE since we will be working on small coding projects. However, for your sake, it is important that you use an editor that has syntax highlighting, lisp indentation, and parentheses highlighting. The most important being parentheses highlighting. This will save a lot of time during programming since Lisp code can build up many parentheses that are semantically significant. Therefore, highlighting can greatly help with checking that the parentheses open and close in the correct places. Recommended Environments : - Emacs (with or without SLIME) Lisp and emacs are closely related so there s a lot of support for Lisp in the emacs environment. SLIME is an extension to emacs that adds additional support. If you re someone who likes a lot of language-related functionality in the editor or plan to use Lisp after this class the time investment of installing and learning SLIME may be worthwhile.

- Vim Though there s less support for Lisp than emacs, vim recognizes the syntax, indentation, and parentheses. Good choice if you re already comfortable with vim. - Allegro IDE Allegro comes with an IDE on their website ( http://franz.com/products/allegrocl/acl_ide.lhtml ). This is a full-fledged eclipse style IDE (I think, I haven t actually used it before). - Notepad++? If you really don t like any of the other options (I admit those editors may have a fairly steep learning curves), this will work since it supports syntax and parenthetical highlighting. Interacting with the REPL The REPL (read-eval-print-loop) allows you to run code dynamically (run one command at a time). Lisp is an interpreted language so it doesn t need to be compiled (though it can be). To start the REPL, from the department servers type the appropriate command ( acl,sbcl, or cmucl ). To exit type :ex or :exit. To load/run a file (like your homework file) type (load filename ). Local Variables/Functions Defining variables locally is important since the scoping of variables and functions in Lisp are global by default. The scopes for variables and functions stated with explicit operators. Variables Local variables are defined using let and let*. let evaluations are all performed in parallel, thus each definition must be independent of one another. let* evaluates each statement in order so that later formulas can use variables defined previously. Functions Local functions are defined using flet and labels. The syntax for these operators are analogous to let and let*, extended to functions, so I recommend getting comfortable with those operators before trying these out. flet evaluates the functions in parallel and the scopes for the definition is only in the body (after the function definitions).

labels has the function scopes encompassing the function definitions so that the local functions can call each other and themselves for recursive processes. Functional Operations There are some paradigms/operations in functional programming that are important for you to know when programming in a language like Lisp. Getting comfortable with these will make your life a lot easier since they simplify many operations. Map (i.e. mapcar) A map takes a function with one argument and a list, then performs the function on each element of the list and returns the result. This is a very common operation (if you think about problems in a functional mindset) which when combined with the next operation that we ll describe can simplify many complex problems into simple abstractions. In Lisp, this is done with mapcar. (mapcar # fn lst) => mapped lst. For example: (mapcar # not (t nil nil t nil)) => (nil t t nil t) The Common Lisp implementation of mapcar can also take multiple lists as arguments and take the elements of each together as separate arguments of the functions. (mapcar # + (1 2 3) (4 5 6)) => (5 7 9) Reduce/Fold (i.e. reduce) A reduce takes a function with two arguments and a list. It applies the function to the accumulated value (can specify initial value or uses the first two elements for accumulation and next value) and the next value in the list and sets that as the new accumulated value. Returns accumulation at the end. (reduce # list (1 2 3 4)) => (((1 2) 3) 4) (reduce # list (1 2 3 4) :initial-value ()) => ((((() 1) 2) 3) 4) (reduce # * (1 2 3 4 5)) => 120 Filter (remove-if remove-if-not) Takes a function with one parameter returning t or nil and a list. Returns the list with only elements that return t for the function. (remove-if-not # even-p (1 2 3 4)) => (1 3) Conditionals

There is the classes if statement in Lisp, but doesn t have support for layering else if s for many levels as in typical procedural languages. Alternatively, Lisp has the cond (conditional) which allows you to list a series of condition followed by execution. For if statements without an else branch, Lisp has when and unless. when is equivalent to if without support of an else branch and unless is equivalent to (when (not )). Evaluating Lisp/Function There are three main functions for evaluating lisp functions or code: eval, funcall, and apply. eval interprets its argument as lisp code and runs it. (e.g. (eval (+ 1 2 3)) => 6) funcall takes a function followed by its arguments (one-by-one) as arguments (e.g. (funcall # + 1 2 3) => 6) apply similar to funcall but takes the arguments as a list. (There are other subtleties, but likely not important for your work here). (e.g. (apply # + (1 2 3)) => 6) Type Tests Types can be checked with a variety of type predicates. A full(?) list can be found https://www.gnu.org/software/emacs/manual/html_node/elisp/type-predicates.html. They are generally suffixed with p to indicate a type predicate, and sometimes have negated varieties that a prefixed with n (short for not). The most important ones for now are: atom - is it an atom (i.e. not a non-empty list) listp - is it a list (including nil) consp - is it a cons cell (i.e. listp without nil) null - is it null (i.e. nil) symbolp - atom that isn t an integer, string, or other special type numberp - is it a number integerp floatp rationalp Equality Lisp has several different equality predicates. They vary on how closely they follow the computer representation of the value vs human semantic value. For details see https://www.cs.cmu.edu/groups/ai/html/cltl/clm/node74.html. In most cases the most intuitive equality predicate is equal. Equality predicates in increasing complexity

eq - only same instances eql - compares integer/character semantics equal - compares strings and lists semantically equalp - compares between number types (integer and float) as others Lisp Weirdness Quotes Quoted symbols will be interpreted as symbols directly, otherwise will be interpreted as some value. If it s a number or a string, this will feel normal. But if put a symbol somewhere without a quote, Lisp will try to interpret it as a variable, parameter or function. Pipes Symbols are by default uppercase only (case-insensitive). Therefore, to specify case-sensitive symbols, use pipes (e.g. Hi There ). As you can see, it also allows spaces and other such symbols that normally are not interpreted as a symbol. Characters Lisp specifies characters with #\. So to specify a character, it must be prefixed as such. Otherwise, it will be interpreted directly as a symbol. Testing I highly recommend looking at the link on my site (cs.rochester.edu/~gkim21/cs444) to the Practical Common Lisp guide to testing in Lisp. It will help you structure your tests transparently and informatively. Debugging Having trouble printing big lists on a single line? Try turning off the pretty printer: (setq *print-pretty* nil). Still not working? Try increasing the margin: (setq *print-right-margin* 10000) When a program crashes, check the backtrace with :bt Practice Problems If you have not used Lisp before, the homework problems are likely too difficult of a starting point for getting started in Lisp. I recommend trying to solve the following problems using recursion and higher-order functions (map/reduce/filter) to get comfortable with Lisp before starting the homework problems. - Count list length without length predicate recursively - Index a list recursively [ (a b c) => ((0 a) (1 b) (2 c)) ] - Implement map - Implement reduce

- All elements in list are true (not nil) - Drop nth element in a list - Depth first search of tree -- (perhaps return the pre-ordering of elements to check it works) Note from Gene : If you ve tried these problems and want to see other plausible solutions I came up with, contact me by email (with your solutions attached) and I can send you what I ve tried. I m making it hard for you guys to get the solutions so that you actually try to solve them on your own before seeing solutions!