מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא.

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

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
CSI2520  Un arbre binaire peut être représentée avec des listes imbriquées a / \ b c / \ d e (a b (c d e)) ou (a (b () ()) (c (d () ()) (e () ())) ou.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Data directed programming Section 2.4, pages ,2.5.2 pages (but with a different example)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 26: Printing and Stuff.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7 1. Outline More list examples Symbols 2.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
CS 3 Final Review Gilbert Chou, Jenny Franco and Colleen Lewis December 14, pm GPB.
מבוא מורחב - שיעור 12 1 Lecture 12 Data directed programming Message passing dotted-tail notation & apply Section 2.4, pages ,2.5.2 pages.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Data directed programming Message passing Section 2.4, pages ,2.5.2 pages (but with.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 7. Outline Symbols Data Directed Programming, Message Passing.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
1 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
מבוא מורחב 1 Multiple representations of data. מבוא מורחב 2 Complex numbers imaginary real i 13 atan(2/3)
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))))))
The “Beauty” of Scheme: Programs as Proofs Q: Is '(abc 123) a list? A: Yes Proof:  '(abc 123) = (cons 'abc (cons 123 '()))  '() is a list, so (cons 123.
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
Functional Programming: Lisp MacLennan Chapter 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.
מבוא מורחב 1 Representing lists as binary trees
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Additional Scheme examples
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
COP4020 Programming Languages
Env. Model Implementation
Lecture 8: Recursion Practice CS200: Computer Science
2.3 Representation Strategies for Data Types
PPL Sequence Interface.
Lecture 18.
Lecture #8 מבוא מורחב.
Lecture 9: The Great Lambda Tree of Knowledge and Power
Mutators for compound data Stack Queue
6.001 SICP Data abstractions
List and list operations (continue).
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
To understand recursion, one must first understand recursion.
Lecture 11: Sorting Grounds and Bubbles
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme בוחן אמצע אביב 2006 פתרון לדוגמא

Split > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (split syms ‘-) ((p l a y) (i n) (e u r o p e) (o r) (i n) (s p a i n))

Split (define (split symbols sep) (define (update sym word-lists) (if (eq? sym sep) (cons ___________________________________ ___________________________________ ) (cons ___________________________________ ___________________________________))) (accumulate update (list null) symbols)) null word-lists (cons sym (car word-lists)) (cdr word-lists)

Replace > (define syms '(p l a y - i n - e u r o p e - o r - i n - s p a i n)) > (replace ‘n ‘m syms) (p l a y – i m – e u r o p e – o r – i m – s p a i m) (define (replace from-sym to-sym symbols) (map )) (lambda (s) (if (eq? from-sym s) to-sym s)) symbols)

Accum-replace > (accum-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s p a i n) (a l a y – i n – e u r o a e – o r – i n – s a a i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (e l e y – i m – e u r o e e – o r – i m – s e e i m)

Accum-replace (define (accum-replace from-to-list symbols) (accumulate (lambda(p syms) ( ________________________________ )) ____________________ from-to-list)) )) replace (car p) (cadr p) syms symbols

Extend-replace > (extend-replace ‘((a e) (n m) (p a)) syms) (p l a y – i n – e u r o p e – o r – i n – s p a i n) (a l a y – i n – e u r o a e – o r – i n – s a a i n) (a l a y – i m – e u r o a e – o r – i m – s a a i m) (a l e y – i m – e u r o a e – o r – i m – s a e i m)

Extend-replace (define (extend-replace from-to-list symbols) (define (scan sym) (let ((from-to (filter _____________________________________ _____________________________________ ))) (if (null? from-to) ___________________________ ___________________________))) (map scan symbols)) (lambda (p) (eq? (car p) sym)) from-to-list sym (cadr (car from-to))

Make-perm > (define p1 (make-perm (list ))) > (p1 1) 4 > (p1 2) 2 > (p1 3) 1 > (p1 4) 3 (define (make-perm lst) ________________________________________________ ________________________________________________ ) (lambda (i) (list-ref lst (- i 1)))

Equal-perms? > (define p1 (make-perm (list ))) > (define p2 (make-perm (list ))) > (define p3 (make-perm (list ))) > (equal-perms? p1 p2 4) #f > (equal-perms? p1 p3 4) #t

Equal-perms? (define (equal-perms? perm1 perm2 k) (accumulate (lambda (n y) __________________________________ ) ____________________________ (integers-between 1 k) )) (and (= (perm1 n) (perm2 n)) y) #t

Inverse-perm > (define p1 (make-perm (list ))) > (define p1-inv (inverse-perm p1)) > (p1 1) 4 > (p1-inv 4) 1 > (p1-inv (p1 2)) 2

Inverse-perm (define (inverse-perm perm) (lambda (n) (define (helper i) (if _____________________________ _____________________________ (helper _____________________ ))) (helper 1))) (= (perm i) n) i (+ i 1)

Insert-at-all-positions > (insert-at-all-positions 'new (list 1 2 3)) ((new 1 2 3) (1 new 2 3) (1 2 new 3) (1 2 3 new)) (define (insert-at-all-positions elem lst) (if (null? lst) (list (list elem)) (cons (cons elem lst) (map (lambda (l) (cons (car lst) l)) (insert-at-all-positions elem (cdr lst))))))

Generate-all-perm-lists (define (generate-all-perm-lists k) (if (= k 1) ______________________________________________ (accumulate _________________________________________ (map _____________________________________ _____________________________________ )))) (list (list 1)) append null (lambda (p) (insert-at-all-positions k p)) (generate-all-perm-lists (- k 1))