;;;;;;;;;;;;;;;;;;;RECURSIVE DEFINITIONS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (couple a y) (if (null? y) () (cons (list a (car y)) (couple a (cdr y)) ) ) ) (couple 1 '(b c d)) ;TEST ; ((1 b) (1 c) (1 d)) (couple 1 '(a b c d)) ;TEST ; ((1 a) (1 b) (1 c) (1 d)) (define (cart2 x y) (if (null? x) () (append (couple (car x) y) (cart2 (cdr x) y) ) ) ) (cart2 '(2 3) '(a b c)) ;TEST ; ((2 a) (2 b) (2 c) (3 a) (3 b) (3 c)) (cart2 '(1 2 3) '(a b c)) ;TEST ;((1 a) (1 b) (1 c) (2 a) (2 b) (2 c) (3 a) (3 b) (3 c)) (length (cart2 '(1 2 3 4) '(a b c d))) ;TEST ; 16 (define dot-couple ;LAMBDA EXPLICIT (lambda (a y) (if (null? y) () (cons (cons a (car y)) (dot-couple a (cdr y)) ) ) ) ) ;(define (dot-couple a y) (map (lambda (l) (cons a l)) y)) (dot-couple 1 '(a b c d)) ;TEST ; ((1 . a) (1 . b) (1 . c) (1 . d)) (define (powerset x) (if (null? x) (list ()) (append (dot-couple (car x) (powerset (cdr x))) (powerset (cdr x)) ) ) ) (powerset '(b c)) ;TEST ; ((b c) (b) (c) ()) (powerset '(a b c)) ;TEST ; ((a b c) (a b) (a c) (a) (b c) (b) (c) ()) (define powerset-eff ;better version (lambda (x) (if (null? x) (list ()) (let ((aux (powerset-eff (cdr x)))) (append (dot-couple (car x) aux) aux ) ) ) ) ) (powerset-eff '(a b c)) ;TEST (length (powerset-eff '(a b c d e))) ;TEST (begin (newline) (display "FUNCTIONS DEFINED:--> ") (write "couple, cart2, dot-couple, powerset, powerset-eff") (newline) ) ;;;;;;;;;;;;;;;;;;; CONTINUATION ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; built-in member ;;; ;;; (member 'a '(a b c)) ;Value: (a b c) ;;; top-level search (define (member e l) (cond ( (null? l) () ) ( (eq? e (car l)) #t ) ( else (member e (cdr l))) ) ) (define member (lambda (e l) (if (null? l) () (or (eq? e (car l)) (member e (cdr l))) ))) (define (remove-first sym lis) (cond ((null? lis) ()) ((eq? sym (car lis)) (cdr lis)) (else (cons (car lis) (remove-first sym (cdr lis)))) ) ) (remove-first 'p '(a p b (p q) p c)) ;(a b (p q) p c) (remove-first 'p '(a b (p q) p c)) ;(a b (p q) c) (define (remove-all sym lis) (cond ((null? lis) ()) ((eq? sym (car lis)) (remove-all sym (cdr lis))) (else (cons (car lis) (remove-all sym (cdr lis)))) ) ) (remove-all 'p '(a p b (p q) p c)) ;(a b (p q) c) ;;; search expression rather than symbol (define (remove-first-exp exp lis) (cond ((null? lis) ()) ((equal? exp (car lis)) (cdr lis)) (else (cons (car lis) (remove-first-exp exp (cdr lis)))) ) ) (remove-first-exp '(p q) '(a p b (p q) p c (p q))) ;(a p b p c (p q)) (remove-first-exp '(p q) '((p q (p q)))) ;( (p q (p q)) ) ;;; all-levels search (define (atom? x) (or (symbol? x) (number? x))) (define (remove-all-* sym lis) (cond ((null? lis) ()) ((atom? (car lis)) (if (eq? sym (car lis)) (remove-all-* sym (cdr lis)) (cons (car lis) (remove-all-* sym (cdr lis))))) (else (cons (remove-all-* sym (car lis)) (remove-all-* sym (cdr lis)))) ) ) (remove-all-* 'p '((p q (p q)))) ; ((q (q))) ;;; (remove-first-* 'p '(a b (p q) p c)) ;(a b (q) p c) ;;; (remove-last 'p '(a b (p q) p c)) ;(a b (q) p c) ;;; (remove-nth 2 'p '(a b (p q) p c)) ;(a b (q) p c) (define (insert n lon) (cond ((null? lon) (list n)) ((> n (car lon)) (cons (car lon) (insert n (cdr lon)))) (else (cons n lon)))) (define (ins-sort lon) (if (null? lon) () (insert (car lon) (ins-sort (cdr lon))))) (ins-sort '(2 3 1 4 7 5)) (define (subset s1 s2) (or (null? s1) (and (member (car s1) s2) (subset (cdr s1) s2)))) (subset '(2 3) '(3 2 1)) (subset '(5) '(3 2 1)) (define (trans1 n) (if (zero? n) '(0) (cons n (trans1 (- n 1))))) (trans1 5) ;(5 4 3 2 1 0) (define (trans2 n) (if (zero? n) '((0)) (cons (list n) (trans2 (- n 1))))) (trans2 5) ;((5) (4) (3) (2) (1) (0)) (define (trans3 n) (if (zero? n) '((0)) (cons (cons n (car (trans3 (- n 1)))) (trans3 (- n 1))))) (trans3 5) ;((5 4 3 2 1 0) (4 3 2 1 0) (3 2 1 0) (2 1 0) (1 0) (0)) (define (trans1.1 n) (if (zero? n) '(0) (append (trans1.1 (- n 1)) (list n)))) (trans1.1 5) ;(0 1 2 3 4 5) (define (trans3.1 n) (if (zero? n) '((0)) (append (trans3.1 (- n 1)) (list (trans1.1 n))))) (trans3.1 5) ;((0) (0 1) (0 1 2) (0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5))