Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Generation of functions with a very flexible signature

Here is a possible solution:

(define (grouping-abstraction-by-predicates collector pred1 pred2) (lambda parameters (let* ((res-3 (group-in-3 parameters pred1 pred2 '() '() '())) (list-of-pred-1-elements (car res-3)) (list-of-pred-2-elements (cadr res-3)) (other-elements (caddr res-3))) (apply collector (list list-of-pred-1-elements list-of-pred-2-elements other-elements))))) (define (group-in-3 parameters pred1 pred2 lst1 lst2 lst3) (cond ((null? parameters) (list (reverse lst1) (reverse lst2) (reverse lst3))) ((pred1 (car parameters)) (group-in-3 (cdr parameters) pred1 pred2 (cons (car parameters) lst1) lst2 lst3)) ((pred2 (car parameters)) (group-in-3 (cdr parameters) pred1 pred2 lst1 (cons (car parameters) lst2) lst3)) (else (group-in-3 (cdr parameters) pred1 pred2 lst1 lst2 (cons (car parameters) lst3))))) (define (sum number-list) (cond ((null? number-list) 0) (else (+ (car number-list) (sum (cdr number-list)))))) (define h (grouping-abstraction-by-predicates (lambda (l1 l2 l3) (list (sum l1) (apply string l2))) number? char?))