FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

Similar documents
FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

INF4820. Common Lisp: Closures and Macros

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

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

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

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

Lisp Basic Example Test Questions

Robot Programming with Lisp

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

A little bit of Lisp

Common LISP Tutorial 1 (Basic)

Common Lisp. Blake McBride

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

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

Announcements. Today s Menu

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

Common LISP-Introduction

A Genetic Algorithm Implementation

(defvar *state* nil "The current state: a list of conditions.")

Recursion & Iteration

Lecture Notes on Lisp A Brief Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Introduction to Lisp

Artificial Intelligence Programming

Lambda Calculus see notes on Lambda Calculus

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

Functional programming with Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 1. ÚVOD DO PŘEDMĚTU, LAMBDA CALCULUS

Functional Programming. Contents

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

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

Review of Functional Programming

A Brief Introduction to Common Lisp

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Allegro CL Certification Program

Functional Programming with Common Lisp

UMBC CMSC 331 Final Exam

19 Machine Learning in Lisp

Functional Programming. Pure Functional Programming

Lecture #2 Kenneth W. Flynn RPI CS

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

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

Object Oriented Programming (OOP)

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

MIDTERM EXAMINATION - CS130 - Spring 2005

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

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

Robot Programming with Lisp

Associative Database Managment WIlensky Chapter 22

Associative Database Managment

User-defined Functions. Conditional Expressions in Scheme

Streams and Lazy Evaluation in Lisp

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

UMBC CMSC 331 Final Exam

CSCI337 Organisation of Programming Languages LISP

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

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

Imperative, OO and Functional Languages A C program is

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

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

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

Documentation for LISP in BASIC

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

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

1 CLWEB INTRODUCTION 1

Evaluating Scheme Expressions

Func%onal Programming in Scheme and Lisp

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

CS 842 Ben Cassell University of Waterloo

Func%onal Programming in Scheme and Lisp

John McCarthy IBM 704

Many versions: Franz, Mac, Inter, Common (now the

Allegro CL Certification Program

CS61A Midterm 2 Review (v1.1)

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

Project 2: Scheme Interpreter

XLISP PLUS. Reference Manual. Version 3.0

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

FP Foundations, Scheme

by the evening of Tuesday, Feb 6

Allegro CL Certification Program

Functional programming techniques

Midterm Examination (Sample Solutions), Cmput 325

Functions, Conditionals & Predicates

Lisp. Versions of LISP

(penguin-rule (if (animal-x is bird) (animal-x can swim)) (then (animal-x is penguin))) (?x is bird)

CSC 533: Programming Languages. Spring 2015

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

A short note on short differentiation programs in lisp, and a comment on logarithmic differentiation

CS450 - Structure of Higher Level Languages

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

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

(defmacro while (condition &body body) `(iterate loop () (if,condition (loop)))))

CS 314 Principles of Programming Languages

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

Introduction to Functional Programming and basic Lisp

Section 10: LISP to Scheme. Evolution of Software Languages

4. Functional Programming Language-Oriented Programming

Transcription:

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti

Variables and constants in Lisp

Global variables and constants Variables: >(defparameter *global* 17) >(defvar *global* 17) global variables are conventionally written enclosed in * Constants: > (defconstant glob-const 11)

Local variables Local variables are created in functions: >(defun foo (x y z) (+ x y z)) and in statements of cycles, etc : >(dotimes (x 10) (format t "~d " x)) operator let: (let (variable*) body-form*) >(let ((a 10) (b 12)) (- b a) (* a b)) 120

Local variables - example (defun foo (x) (format t "Parameter: ~a~%" x) ; <------ x is arg (let ((x 2)) ; (format t "Outer LET: ~a~%" x) ; <---- x is 2 (let ((x 3)) ; (format t "Inner LET: ~a~%" x)) ; <--x=3 (format t "Outer LET: ~a~%" x)) ; (format t "Parameter: ~a~%" x)) ; CL-USER> (foo 1) Parameter: 1 Outer LET: 2 Inner LET: 3 Outer LET: 2 Parameter: 1 NIL

Assignment Macro setf: (setf x 1) (setf y 2) (setf x 1 y 2) (incf x) === (setf x (+ x 1)) (decf x) === (setf x (- x 1)) (incf x 10) === (setf x (+ x 10))

More on functions - parameters

Optional arguments of functions Optional parameters &optional: (defun foo (a b &optional c d) (list a b c d)) (foo 1 2) ==> (1 2 NIL NIL) (foo 1 2 3) ==> (1 2 3 NIL) (foo 1 2 3 4) ==> (1 2 3 4) (defun foo (a &optional (b 10)) (list a b)) (foo 1 2) ==> (1 2) (foo 1) ==> (1 10)

Rest and keywords parameters Rest parameters as a list &rest: (defun + (&rest numbers)...) Optional keywords parameters &keyword: (defun foo (&key a b c) (list a b c)) (foo) ==> (NIL NIL NIL) (foo :a 1) ==> (1 NIL NIL) (foo :b 1) ==> (NIL 1 NIL) (foo :c 1) ==> (NIL NIL 1) (foo :a 1 :c 3) ==> (1 NIL 3) (foo :a 1 :b 2 :c 3) ==> (1 2 3) (foo :a 1 :c 3 :b 2) ==> (1 2 3)

Function Return Values return-from: (defun foo (n) (dotimes (i 10) (dotimes (j 10) (when (> (* i j) n) (return-from foo (list i j)))))) Note. RETURN-FROMs are used much less frequently in Lisp than return statements in C-derived languages

Blocks

Simple sequencing block returns (prog1 form-1 form-2... form-n ) form-1 (prog2 form-1 form-2... form-n ) form-2 (progn form-1 form-2... form-n ) form-n

block and return-from once more (block name form-1 form-2... form-n ) (return-from name val ) Block is like progn, returning the last value > (block test 1 2 3 4 5) 5 > (block test 1 2 (return-from test 33) 4 5) 33 > (block nil 1 2 (return 33) 4 5) 33 > (block a (+ (block b 1 2 (return-from b 33) 4) (block c 5 6 (return-from a 77) 8))) 77 3.13

More on functions functions as data

Function operator operator FUNCTION (aka #) provides the mechanism for getting a function object CL-USER> (defun foo (x) (* 2 x)) FOO CL-USER> (function foo) #<Interpreted Function FOO> CL-USER> #'foo #<Interpreted Function FOO> Functions can be parameters of other functions

Funcall and apply operators FUNCALL is the one to use when you know the number of arguments you're going to pass to the function: (foo 1 2 3) === (funcall #'foo 1 2 3) In APPLY the second argument after the function object, instead of individual arguments, expects to be a list. (foo 1 2 3) === (apply #'foo (1 2 3))

Example - functions as data (defun plot (fn min max step) (loop for i from min to max by step do (loop repeat (funcall fn i) do (format t "*")) (format t "~%"))) CL-USER> (plot #'exp 0 4 1/2) * * ** **** ******* ************ ******************** ********************************* ****************************************************** NIL

Example - functions as data (defun plot (fn min max step) (loop for i from min to max by step do (loop repeat (funcall fn i) do (format t "*")) (format t "~%"))) CL-USER> (plot #'exp 0 4 1/2) * * ** **** ******* ************ ******************** ********************************* ****************************************************** NIL

Anonymous functions

Anonymous (unnamed) functions General syntax: (lambda (parameters) body) (funcall #'(lambda (x y) (+ x y)) 2 3) ==> 5 useful when one needs to pass a function as an argument to another function and the function you need to pass is simple enough to express inline

Anonymous functions - example CL-USER> (plot #'double 0 10 1) ** **** ****** ******** ********** ************ ************** **************** ****************** ******************** NIL CL-USER> (plot #'(lambda (x) (* 2 x)) 0 10 1) ** **** ****** ******** ********** ************ ************** **************** ****************** ******************** NIL

Iterations mapping functionals, macros-iteration cycles

Mapping Functionals Functional = a function with functions in arguments. (defun sum-f (FF S) (cond ((null S) 0) ((+ (funcall FF (car S)) (sum-f FF (cdr S)) )))) (sum-f #'square '(1 2 3)) ; --> 14 To apply the same function to all elements of a list: (defun transform (FF S) (cond ((null S) NIL) ((cons (funcall FF (car S)) (transform FF (cdr S)) )))) (transform #'square '(1 2 3)) ; --> (1 4 9) Very common transformation of one list (or more) standard mapping functionals are in Lisp!

(mapcar #'square '(1 2 3)) ; (1 4 9) (mapcar #'cons '(1 2 3) '((A) (B) (C))) (mapcar #'list '(1 2) '(A B) '(X Y)) A common format for standard mapping functionals: (mapcar function list-1 list-2... list-n ) mapcar/mapc operate on successive elements of the lists. The iteration terminates when the shortest list runs out, and excess elements in other lists are ignored. The value returned by mapcar is a list of the results of successive calls to function. The value of mapc is list-1. maplist/mapl are similar to mapcar/mapc but operate on sublists, maplist returns a list of the results, mapl just list-1. mapcan/mapcon are similar to mapcar/maplist but the results are combined into a list as by nconc

Mapping Functionals examples CL-USER 1 > (mapcar #'list '(1 2 3) '(4 5 6 7) '(9 8 1 2 3 4)) ((1 4 9) (2 5 8) (3 6 1)) CL-USER 2 > (mapcan #'list '(1 2 3) '(4 5 6 7) '(9 8 1 2 3 4)) (1 4 9 2 5 8 3 6 1) CL-USER 3 > (mapc #'list '(1 2 3) '(4 5 6 7) '(9 8 1 2 3 4)) (1 2 3) CL-USER 4 > (maplist #'list '(1 2 3) '(4 5 6 7) '(9 8 1 2 3 4)) (((1 2 3) (4 5 6 7) (9 8 1 2 3 4)) ((2 3) (5 6 7) (8 1 2 3 4)) ((3) (6 7) (1 2 3 4)))

Mapping Functionals another example Maximal element using mapc (defun MyMax (S) (let ((MaxEl (car S))) (mapc # (lambda (X) (if (> X MaxEl)(setf MaxEl X))) S) ; or (cdr S) MaxEl )) Note. Mapc is used but for its side effects its returning value is not interesting in this case.

Iterations - dolist Dolist: (dolist (var list-form) body-form*) CL-USER> (dolist (x '(1 2 3)) (print x)) 1 2 3 NIL CL-USER> (dolist (x '(1 2 3)) (print x) (if (> x 1) (return))) 1 2 NIL

Iterations - dotimes Dotimes: (dotimes (var count-form) body-form*) CL-USER> (dotimes (i 4) (print i)) 0 1 2 3 NIL

Iterations - do Do: (do (variable-definition*) (end-test-form result-form*) statement*) (var init-form step-form) (do ((i 0 (1+ i))) ((>= i 4)) (print i))

Iterations - loop loop: (loop body-form*) Basic unfinite loop: (loop (when (> (get-universal-time) *some-future-date*) (return)) (format t "Waiting~%") (sleep 60)) Many other variants, for example: (loop for i from 1 to 10 collecting i) ==> (1 2 3 4 5 6 7 8 9 10)

An example for as a conclusion: function cc-list in five different ways Let s define a function cc-list that does the same thing as copy-list 1. (defun cc-list (list) (let ((result nil)) (dolist (item list result) (setf result (append result (list item)))))) 1st implementation uses append to put elements onto the end of the list. It traverses the entire partial list each time quadratic running time.

An example for as a conclusion: function cc-list in five different ways 2. (defun cc-list (list) (let ((result nil)) (dolist (item list (nreverse result)) (push item result)))) 2nd implementation goes through the list twice: first to build up the list in reverse order, and then to reverse it. It has linear running time.

3. (defun cc-list (list) (let ((result (make-list (length list)))) (do ((original list (cdr original)) (new result (cdr new))) ((null original) result) (setf (car new) (car original))))) 4. (defun cc-list (list) (mapcar #`identity list))

5. (defun cc-list (list) (loop for x in list collect x)) 3rd, 4th, and 5th implementations: efficiency usually similar to the 2nd one, depending on the Lisp implementation. The 4th and 5th implementations are the easiest to understand.