Like most people, I've built up a small library of functions that i seem to use over and over again, and are not specific to any individual application that I write.. I was considering makeing a planet package out of them, but before I do, If anyone has comments on them, I'd love to hear them. I'm guessing i'm not the first person to have written some of these functions.
<br><br>Here are the first few that I'm considering of posting: {examples on how to use them follows most of the non-obvious functions}<br><br>;sexp functions<br>;;sexp->string<br>;;string->sexp<br><br>(define (sexp->string x)
<br> (let ((string-x (open-output-string)))<br> (write x string-x)<br> (get-output-string string-x)))<br><br>(define (string->sexp x) (read (open-input-string x)))<br><br><br><br><br><br>;map related functions<br>
;;maprec<br>;;any-rec<br>;;unique<br>;;compose-functions-by-fold<br>;;make-combinations<br>;;make-permutations<br>;;flatten<br><br><br>;;maprec maps func to all elements of a tree, but only the elements (no lists)<br>(define (maprec func tree)
<br> (if (not (list? tree))<br> (func tree)<br> (map (lambda (x) (maprec func x)) tree)))<br><br><br><br>(define (any-rec func tree)<br> (if (list? tree)<br> (any (lambda (x) (any-rec func x)) tree)<br> (func tree)))
<br><br>(register-test-case `map-related-tests<br> (make-test-case "any-rec-1"<br> (assert-equal? (any-rec even? `(2 3 9 4 (3 8 3 4 6 5 5) 1 12))<br> #t)))<br><br>
<br><br>(define (unique list-in)<br> (cond ((null? list-in)<br> list-in)<br> (#t<br> (cons (first list-in)<br> (unique (filter (lambda (x) (not (equal? x (first list-in))))<br> (rest list-in)))))))
<br><br>(register-test-case `map-related-tests<br> (make-test-case "unique-1"<br> (assert-equal? (unique `(a b c r c k u f a i b e c e gf))<br> `(a b c r k u f i e gf))))
<br><br><br><br><br>(define compose-functions-by-fold<br> (lambda (list-of-functions)<br> (lambda (start-value)<br> (fold (lambda (x y) (x y)) start-value list-of-functions))))<br><br>(register-test-case<br>
`map-related-tests<br> (make-test-case "compose-functions-by-fold-1"<br> (assert-equal? [(compose-functions-by-fold (list (lambda (i) (display "1") (+ 1 i))<br> (lambda (i) (display "2") (+ 1 i))
<br> (lambda (i) (display "3") (+ 1 i))<br> (lambda (i) (display "4") (+ 1 i))))
<br> 7]<br> 11)))<br><br><br><br><br><br>(define make-combinations<br> (lambda (list-of-lists)<br> ;(display #\newline)<br> (write list-of-lists)<br>
(if (= 1 (length list-of-lists))<br> (begin<br> ;(display "tick")<br> (map list (first list-of-lists)))<br> (begin<br> ;(mdisplay "list of lists: " list-of-lists "\nrest of lost-of-lists: " (rest list-of-lists))
<br> (apply append<br> (map (lambda (element-of-first-list)<br> (map (lambda (z) (cons element-of-first-list z))<br> (make-combinations (rest list-of-lists))))
<br> (first list-of-lists)))))))<br><br>(register-test-case `map-related-tests<br> (make-test-case "make-combinations-2"<br> (assert-equal? (make-combinations `[(a b c) (1 2 3) (x y)])
<br> `((a 1 x) (a 1 y) (a 2 x) (a 2 y) (a 3 x) (a 3 y)<br> (b 1 x) (b 1 y) (b 2 x) (b 2 y) (b 3 x) (b 3 y)<br> (c 1 x) (c 1 y) (c 2 x) (c 2 y) (c 3 x) (c 3 y)))))
<br><br><br><br><br><br>(define (make-permutations list-of-elements)<br> (if (= 1 (length list-of-elements))<br> (list list-of-elements)<br> (apply append<br> (map<br> (lambda (element)
<br> (map<br> (lambda (x) (cons element x))<br> (make-permutations (filter (lambda (z) (not (equal? element z))) list-of-elements))))<br> list-of-elements))))<br>
<br>(register-test-case `map-related-tests<br> (make-test-case "make-permutations-1"<br> (assert-equal? (make-permutations `(a b))<br> `((a b) (b a)))))
<br><br>(register-test-case `map-related-tests<br> (make-test-case "make-permutations-2"<br> (assert-equal? (make-permutations `(a b c))<br> `((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)))))
<br><br><br><br><br>(define (flatten tree)<br> (fold (lambda (item list-so-far)<br> (if (not (list? item))<br> (append list-so-far (list item))<br> (append list-so-far (flatten item))))
<br> `()<br> tree))<br><br>(register-test-case `map-related-tests<br> (make-test-case "flatten-1"<br> (assert-equal? (flatten `(2 (3 (9) 4) (3 (8 (3 (4 6))) 5 5) 1 12))<br> `(2 3 9 4 3 8 3 4 6 5 5 1 12))))
<br><br><br><br><br><br>Corey<br clear="all"><br>-- <br>((lambda (y) (y y)) (lambda (y) (y y)))