Robot Programming with Lisp



Similar documents
Universitetet i Linköping Institutionen för datavetenskap Anders Haraldsson

Chapter 15 Functional Programming Languages

MongoDB. Or how I learned to stop worrying and love the database. Mathias Stearn. N*SQL Berlin October 22th, gen

CSCI 3136 Principles of Programming Languages

Programming Languages

Programming Languages in Artificial Intelligence

How To Program In Scheme (Prolog)

DATA MINING WITH HADOOP AND HIVE Introduction to Architecture

6.170 Tutorial 3 - Ruby Basics

Basic Lisp Operations

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Introduction to Python

CSE 130 Programming Language Principles & Paradigms

Lecture 1: Introduction

What is a programming language?


CSE 307: Principles of Programming Languages

1 Idioms, Patterns, and Programming

Big Data: Big N. V.C Note. December 2, 2014

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

Big Data Storage, Management and challenges. Ahmed Ali-Eldin

Develop PHP mobile apps with Zend Framework

dictionary find definition word definition book index find relevant pages term list of page numbers

opalang - Rapid & Secure Web Development

Fall 2012 Q530. Programming for Cognitive Science

Functional Programming

Semester Review. CSC 301, Fall 2015

Introduction to Parallel Programming and MapReduce

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Software. Programming Language. Software. Instructor Özgür ZEYDAN. Bülent Ecevit University Department of Environmental Engineering

Functional Programming in C++11

Braindumps.C questions

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Hadoop and Map-reduce computing

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Numerical Analysis. Professor Donna Calhoun. Fall 2013 Math 465/565. Office : MG241A Office Hours : Wednesday 10:00-12:00 and 1:00-3:00

Semantic Analysis: Types and Type Checking

Computational Mathematics with Python

Beyond the Mouse A Short Course on Programming

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

General Introduction

FREE computing using Amazon EC2

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

NoSQL replacement for SQLite (for Beatstream) Antti-Jussi Kovalainen Seminar OHJ-1860: NoSQL databases

Getting Started with the Internet Communications Engine

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

The C Programming Language course syllabus associate level

Introduction to Hadoop

Slides from INF3331 lectures - web programming in Python

Lambda Architecture. CSCI 5828: Foundations of Software Engineering Lecture 29 12/09/2014

16.1 MAPREDUCE. For personal use only, not for distribution. 333

Evolution of the Major Programming Languages

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Attacking MongoDB. Firstov Mihail

Computational Mathematics with Python

Computational Mathematics with Python

PostgreSQL Functions By Example

Chapter 5. Recursion. Data Structures and Algorithms in Java

The Smalltalk Programming Language. Beatrice Åkerblom

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

CS 106 Introduction to Computer Science I

Parameter passing in LISP

Interactive Data Visualization for the Web Scott Murray

Lecture 5: Java Fundamentals III

Java with Eclipse: Setup & Getting Started

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Random Fibonacci-type Sequences in Online Gambling

alchemy webapp framework Introduction What is alchemy?

Data Mining Algorithms Parallelizing in Functional Programming Language for Execution in Cluster

Certified Cloud Computing Professional VS-1067

Automated Machine Learning For Autonomic Computing

the missing log collector Treasure Data, Inc. Muga Nishizawa

FUNCTIONAL DATA ANALYSIS: INTRO TO R s FDA

Visualising Java Data Structures as Graphs

CSC230 Getting Starting in C. Tyler Bletsch

Python Programming: An Introduction to Computer Science

Visualizing MongoDB Objects in Concept and Practice

Introduction to NoSQL Databases and MapReduce. Tore Risch Information Technology Uppsala University

Python 2 and 3 compatibility testing via optional run-time type checking

CS 40 Computing for the Web

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

C H A P T E R Regular Expressions regular expression

PHP MySQL vs. Unity. Introduction. The PHP side. The Unity side

Computer Programming Tutorial

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus sandhaus@cs.nyu.edu evan@nytimes.com

Transcription:

4. More Functional Programming: Map/Reduce, Recursions Institute for Artificial Universität Bremen

Outline 2

Mapping Mapping in functional programming is the process of applying a function to all members of a list, returning a list of results. Supported in most functional programming languages and, in addition C++ (STL) JavaScript 1.6+ Matlab Java 8+ PHP 4.0+ Perl Python 1.0+ Ruby Prolog C# 3.0+ Mathematica Smalltalk,... In some of the languages listed the implementation is limited and not elegant. 3

Mapping [2] mapcar is the standard mapping function in Common Lisp. mapcar function list-1 &rest more-lists result-list Apply function to elements of list-1. Return list of function return values. mapcar CL-USER> (mapcar #'abs '(-2 6-24 4.6-0.2d0-1/5)) (2 6 24 4.6 0.2d0 1/5) CL-USER> (mapcar #'list '(1 2 3 4)) ((1) (2) (3) (4)) CL-USER> (mapcar #'second '((1 2 3) (a b c) (10/3 20/3 30/3)))? CL-USER> (mapcar #'+ '(1 2 3 4 5) '(10 20 30 40))? CL-USER> (mapcar #'cons '(a b c) '(1 2 3))? CL-USER> (mapcar (lambda (x) (expt 10 x)) '(2 3 4))? 4

Mapping [2] mapcar is the standard mapping function in Common Lisp. mapcar function list-1 &rest more-lists result-list Apply function to elements of list-1. Return list of function return values. mapcar CL-USER> (mapcar #'abs '(-2 6-24 4.6-0.2d0-1/5)) (2 6 24 4.6 0.2d0 1/5) CL-USER> (mapcar #'list '(1 2 3 4)) ((1) (2) (3) (4)) CL-USER> (mapcar #'second '((1 2 3) (a b c) (10/3 20/3 30/3))) (2 B 20/3) CL-USER> (mapcar #'+ '(1 2 3 4 5) '(10 20 30 40)) (11 22 33 44) CL-USER> (mapcar #'cons '(a b c) '(1 2 3)) ((A. 1) (B. 2) (C. 3)) CL-USER> (mapcar (lambda (x) (expt 10 x)) '(2 3 4)) (100 1000 10000) 5

Mapping [3] mapc is mostly used for functions with side effects. mapc function list-1 &rest more-lists list-1 mapc CL-USER> (mapc #'set '(*a* *b* *c*) '(1 2 3)) (*A* *B* *C*) CL-USER> *c* 3 CL-USER> (mapc #'format '(t t) '("hello, " "world~%")) hello, world (T T) CL-USER> (mapc (alexandria:curry #'format t) '("hello, " "world~%")) hello, world ("hello~%" "world~%") CL-USER> (mapc (alexandria:curry #'format t "~a ") '(1 2 3 4)) 1 2 3 4 (1 2 3 4) CL-USER> (let (temp) (mapc (lambda (x) (push x temp)) '(1 2 3)) temp) 6

Mapping [4] mapcan combines the results using nconc instead of list. mapcan function list-1 &rest more-lists concatenated-results If the results are not lists, the consequences are undefined. nconc vs list CL-USER> (list '(1 2) nil '(3 45) '(4 8) nil) ((1 2) NIL (3 45) (4 8) NIL) CL-USER> (nconc '(1 2) nil '(3 45) '(4 8) nil) (1 2 3 45 4 8) CL-USER> (nconc 1 2 nil 3 45 4 8 nil) ; Evaluation aborted on #<TYPE-ERROR expected-type: LIST datum: 1>. CL-USER> (let ((first-list (list 1 2 3)) (second-list (list 4 5))) (values (nconc first-list second-list) first-list second-list)) (1 2 3 4 5) (1 2 3 4 5) (4 5) 7

Mapping [4] mapcan combines the results using nconc instead of list. mapcan function list-1 &rest more-lists concatenated-results If the results are not lists, the consequences are undefined. mapcan CL-USER> (mapcar #'list '(1 2 3)) ((1) (2) (3)) CL-USER> (mapcan #'list '(1 2 3)) (1 2 3) CL-USER> (mapcan #'alexandria:iota '(1 2 3)) (0 0 1 0 1 2) CL-USER> (mapcan (lambda (x) (when (numberp x) (list x))) '(4 n 1/3 ":)")) (4 1/3) 8

Mapping [5] maplist, mapl and mapcon operate on sublists of the input list. maplist function list-1 &rest more-lists result-list maplist CL-USER> (maplist #'identity '(1 2 3)) ((1 2 3) (2 3) (3)) CL-USER> (maplist (lambda (x) (when (>= (length x) 2) (cond ((< (first x) (second x)) 1) ((> (first x) (second x)) -1) (t 0)))) '(0 0 1 1 1 0 1 0 1 0 0 1))............ (0 1 0 0-1 1-1 1-1 0 1 NIL)........... CL-USER> (maplist (lambda (a-list) (apply #'* a-list)) '(5 4 3 2 1)) (120 24 6 2 1) 9

Mapping [5] maplist, mapl and mapcon operate on sublists of the input list. mapl function list-1 &rest more-lists list-1 mapcon function list-1 &rest more-lists concatenated-results mapl CL-USER> (let (temp) (mapl (lambda (x) (push x temp)) '(1 2 3)) temp) ((3) (2 3) (1 2 3)) mapcon CL-USER> (mapcon #'reverse '(4 3 2 1)) (1 2 3 4 1 2 3 1 2 1) CL-USER> (mapcon #'identity '(1 2 3 4)) ; Evaluation aborted on NIL. 10

Mapping [6] map is a generalization of mapcar for sequences (lists and vectors). map result-type function first-sequence &rest more-sequences result map CL-USER> (mapcar #'+ #(1 2 3) #(10 20 30)) The value #(1 2 3) is not of type LIST. CL-USER> (map 'vector #'+ #(1 2 3) #(10 20 30)) #(11 22 33) CL-USER> (map 'list #'+ '(1 2 3) '(10 20 30)) (11 22 33) CL-USER> (map 'list #'identity '(#\h #\e #\l #\l #\o)) (#\h #\e #\l #\l #\o) CL-USER> (map 'string #'identity '(#\h #\e #\l #\l #\o)) "hello" 11

Reduction reduce function sequence &key key from-end start end initial-value result Uses a binary operation, function, to combine the elements of sequence. reduce CL-USER> (reduce (lambda (x y) (list x y)) '(1 2 3 4)) (((1 2) 3) 4) CL-USER> (reduce (lambda (x y) (format t "~a ~a~%" x y)) '(1 2 3 4)) 1 2 NIL 3 NIL 4 CL-USER> (reduce #'+ '()) ;? CL-USER> (reduce #'cons '(3 2 1 nil)) (((3. 2). 1)) CL-USER> (reduce #'cons '(3 2 1) :from-end t :initial-value nil) (3 2 1) CL-USER> (reduce #'+ '((1 2) (3 4) (5 6)) :key #'first :start 1 :initial-value -10) -2 ; = -10 + 3 + 5 12

MapReduce Google s MapReduce is a programming paradigm used mostly in huge databases for distributed processing. It was originally used for updating the index of the WWW in their search engine. Currently supported by AWS, MongoDB,... Inspired by the map and reduce paradigms of functional programming. https://en.wikipedia.org/wiki/mapreduce 13

MapReduce [2] Example Task: calculate at which time interval the number of travelers on the tram is the highest (intervals are early morning, late morning,...) Database: per interval hourly entries on number of travelers (e.g. db_early_morning: 6:00 Tram6 100, 7:00 Tram8 120) Map step: per DB, go through tram lines and sum up travelers: DB1 early morning: (Tram6 2000) (Tram8 1000)... DB6 late night: (Tram6 200) (Tram4 500)... Reduce: calculate maximum of all databases for each tram line: Tram6 3000 (late morning) Tram8 1300 (early evening)... 14

Local Function Definitions flet CL-USER> (defun some-pseudo-code () (flet ((do-something (arg-1) (format t "doing something ~a now...~%" arg-1))) (format t "hello.~%") (do-something "nice") (format t "hello once again.~%") (do-something "evil"))) SOME-PSEUDO-CODE CL-USER> (some-pseudo-code) hello. doing something nice now... hello once again. doing something evil now... NIL CL-USER> (do-something) ; Evaluation aborted on #<UNDEFINED-FUNCTION DO-SOMETHING {101C7A9213}>. 15

Local Function Definitions [2] flet, labels CL-USER> (let* ((lexical-var 304) (some-lambda (lambda () (+ lexical-var 100)))) (let ((lexical-var 4)) (funcall some-lambda))) ;? CL-USER> (let ((lexical-var 304)) (flet ((some-function () (+ lexical-var 100))) (let ((lexical-var 4)) (some-function)))) ;? 16

Local Function Definitions [2] flet, labels CL-USER> (let* ((lexical-var 304) (some-lambda (lambda () (+ lexical-var 100)))) (let ((lexical-var 4)) (funcall some-lambda))) 404 CL-USER> (let ((lexical-var 304)) (flet ((some-function () (+ lexical-var 100))) (let ((lexical-var 4)) (some-function)))) 404 CL-USER> (labels ((call-me () (format t "inside CALL-ME~%")) (calling () (format t "inside CALLING~%") (call-me))) (calling)) inside CALLING inside CALL-ME 17

Recursion Primitive Example CL-USER> (defun dummy-recursion (my-list) (when my-list (dummy-recursion (rest my-list)))) DUMMY-RECURSION CL-USER> (trace dummy-recursion) (dummy-recursion '(1 2 3 4 5)) 0: (DUMMY-RECURSION (1 2 3 4 5)) 1: (DUMMY-RECURSION (2 3 4 5)) 2: (DUMMY-RECURSION (3 4 5)) 3: (DUMMY-RECURSION (4 5)) 4: (DUMMY-RECURSION (5)) 5: (DUMMY-RECURSION NIL) 5: DUMMY-RECURSION returned NIL 4: DUMMY-RECURSION returned NIL 3: DUMMY-RECURSION returned NIL 2: DUMMY-RECURSION returned NIL 1: DUMMY-RECURSION returned NIL 0: DUMMY-RECURSION returned NIL 18

Recursion [2] Primitive Example #2 CL-USER> (defun print-list (list) (format t "Inside (print-list ~a)... " list) (when list (format t "~a~%" (first list)) (print-list (rest list)))) PRINT-LIST CL-USER> (print-list '(1 2 3)) Inside (print-list (1 2 3))... 1 Inside (print-list (2 3))... 2 Inside (print-list (3))... 3 Inside (print-list NIL)... CL-USER> (mapl (lambda (list) (format t "List: ~a... ~a~%" list (first list))) '(1 2 3)) List: (1 2 3)... 1 List: (2 3)... 2 List: (3)... 3 (1 2 3) 19

Recursion [3] Length of a List CL-USER> (defun my-length (a-list) (if (null a-list) 0 (+ 1 (my-length (rest a-list))))) MY-LENGTH CL-USER> (trace my-length) (my-length '(5 a 3 8)) 0: (MY-LENGTH (5 A 3 8)) 1: (MY-LENGTH (A 3 8)) 2: (MY-LENGTH (3 8)) 3: (MY-LENGTH (8)) 4: (MY-LENGTH NIL) 4: MY-LENGTH returned 0 3: MY-LENGTH returned 1 2: MY-LENGTH returned 2 1: MY-LENGTH returned 3 0: MY-LENGTH returned 4 4 20

Recursion [4] Tail Recursion Optimization CL-USER> (defun my-length-inner (a-list accumulator) (if (null a-list) accumulator (my-length-inner (rest a-list) (1+ accumulator)))) MY-LENGTH-INNER CL-USER> (my-length-inner '(5 a 3 8) 0) 4 CL-USER> (defun my-length-optimal (a-list) (my-length-inner a-list 0)) MY-LENGTH-OPTIMAL CL-USER> (trace my-length-inner) (MY-LENGTH-INNER) CL-USER> (my-length-optimal '(5 a 3 8))... CL-USER> (untrace my-length my-length-inner) T 21

Recursion [5] Tail Recursion Optimization: Second Try CL-USER>, restart-inferior-lisp CL-USER> (proclaim '(optimize speed)) ; No value CL-USER> (defun my-length-inner (a-list accumulator) (if (null a-list) accumulator (my-length-inner (rest a-list) (1+ accumulator)))) CL-USER> (defun my-length-optimal (a-list) (my-length-inner a-list 0)) CL-USER> (trace my-length-optimal my-length-inner) (MY-LENGTH-OPTIMAL MY-LENGTH-INNER) CL-USER> (my-length-optimal '(5 a 3 8)) 0: (MY-LENGTH-OPTIMAL (5 A 3 8)) 1: (MY-LENGTH-INNER (5 A 3 8) 0) 1: MY-LENGTH-INNER returned 4 0: MY-LENGTH-OPTIMAL returned 4 4 22

Recursion [6] What Does This Function Do? CL-USER> (defun sigma (n) (labels ((sig (c n) (declare (type fixnum n c)) (if (zerop n) c (sig (the fixnum (+ n c)) (the fixnum (- n 1)))))) (sig 0 n))) SIGMA CL-USER> (trace sigma) (SIGMA) CL-USER> (sigma 5) 0: (SIGMA 5) 0: SIGMA returned 15 15 (declare (type typespec var*) (the return-value-type form) 23

Links Artificial Functional programmer Bible (available for download): http://www.paulgraham.com/onlisp.html 24

Outline 25

Info Summary Assignment code: REPO/assignment_4/src/*.lisp Assignment due: 10.11, Tuesday, 08:00 AM German time Next class: 10.11, 14:15, room TAB 1.58 (1. OG) 26

Q & A Thanks for your attention! 27