Programming Style and Technique

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Writing LISP functions. 2 COND Rule 1: Unless the function is extremely simple, begin with a COND If you can write the function body in one line, do it.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
1 Part 1 The Prolog Language Chapter 8 Programming Style and Technique.
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Scheme for Python Programmers CSE Valeria Montero.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Monkey and Bananas Exercise Notes on Exercise 3.10 of Bratko For CSCE 580 Sp03.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Conditionals and Recursion "To iterate is human, to recurse divine." - L. Peter Deutsch.
FATIH UNIVERSITY Department of Computer Engineering Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03.
331 Final Spring Details 6-8 pm next Monday Comprehensive with more emphasis on material since the midterm Study example finals and midterm exams.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Functional Programming
Additional Scheme examples
Section 15.4, 15.6 plus other materials
Tail Recursion.
CS 550 Programming Languages Jeremy Johnson
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
A lightening tour in 45 minutes
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Getting Started with Lisp
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
6.001 SICP Data abstractions
Writing LISP functions
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture #8 מבוא מורחב.
CS 36 – Chapter 11 Functional programming Features Practice
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Lisp: Using Functions as Data
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Common Lisp II.
Programming Languages
Lisp.
More Scheme CS 331.
Defining Macros in Scheme
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

Programming Style and Technique Notes for Ch.8 of Bratko For CSCE 580 Sp03 Marco Valtorta

Use of Recursion: maplist maplist is an example of a “higher-order function”: a function that applies to another function In the original LISP (McCarthy, 1960): ((label maplist (lambda (x f) (cond ((null x) ‘( )) (‘t (cons (f (car x)) (maplist (cdr x) f)))))) Maplist( List, F, NewList) if NewList contains the result of applying F to each element of List, in order The function F the binary relation representing the function we want to apply

maplist II Boundary (base) case: List = [ ] if List = [ ] then NewList = [ ] General case: List = [ X|Tail] To transform a list of the form [ X|Tail], do transform the item X by F, obtaining New X, and transform the list Tail obtaining NewTail the whole transformed list if [ NewX|NewTail] See ch8_1.pl renamed maplist1, because maplist is built in, with slightly different meaning call/2 can be used for a more functional (less relational) style

Generalization Generalization allows us to use recursion Example: eight queens without generalization, no integer to recur on! Define nqueens( Pos,N) if N queens are safe in position Pos Base case: N = 0: trivial General case: N > 0 achieve a safe configuration of N-1 queens add the remaining queen so that it does attack eightqueens( Pos) :- nqueens( Pos,8).

Debugging Most Prolog interpreters and compilers use a four-port model for goals call foo/n succeed fail retry

Debugging Example ch3_1.pl1 [trace] 1 ?- del( a,[a,b,a],L) See Section 2.9 of SWI-Prolog manual for some extensions

Improving Efficiency Eight queens, again: Map coloring using a more informed initial ordering of queen coordinate values helps, as described in the comments at the end of ex4_6.pl Map coloring start with countries that have many neighbors Warnsdorff’s heuristic for the knight’s tour

Difference Lists concat( A1-Z1,Z1-Z2,A1-Z2). See UseDefK4.ppt

Accumulators sumlist See UseDefK4.ppt