home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-12-28 | 1.1 KB | 42 lines | [TEXT/Help] |
- {••• Are Multiple Values useful ?…… Let's emulate them with conses •••}
-
- ;;; the Common-Lisp values form
- (define (mret | l) l)
-
- ;;; To apply a multiple function on a multiple value,
- ;;; returns a… multiple value :-\
-
- (define (retfrom v | l)
- (retfrom2 v l))
-
- (define (retfrom2 v l)
- (cond (null? l) ()
- (cons ((0 l) (0 v))(retfrom2 (-1 v) (-1 l)))))
-
- ;;; A small Test
- ;;; count: Counts the number of occ. of a and b in l in one pass
-
- ;;; A first classical example with 2 bags: nca and ncb
- ;;; to get a terminal recursion
-
- (define (count a b l nca ncb)
- (cond (null? l) (list nca ncb)
- (=? a (0 l)) (count a b (-1 l) (1+ nca) ncb)
- (=? b (0 l)) (count a b (-1 l) nca (1+ ncb))
- (count a b (-1 l) nca ncb)))
-
- (count 1 2 '(1 1 2 2 3 1 2 1 5 4 1) 0 0)
- { = (5 3) }
-
- ;;; The two bags are now hidden in the multiple values
- ;;; looks more pretty !
-
- (define (count2 a b l)
- (cond (null? l) (mret 0 0)
- (=? a (0 l)) (retfrom (count2 a b (-1 l)) 1+ I)
- (=? b (0 l)) (retfrom (count2 a b (-1 l)) I 1+)
- (count2 a b (-1 l))))
-
- (count2 1 2 '(1 1 2 2 3 1 2 1 5 4 1))
- { = (5 3) }
-