add map/values from carl, and `debug' prints exceptions
svn: r18197
This commit is contained in:
parent
f7447ee6ae
commit
d9fabc314b
|
@ -12,7 +12,10 @@
|
||||||
(for/list ([arg 'args]
|
(for/list ([arg 'args]
|
||||||
[val l])
|
[val l])
|
||||||
(printf "\t~a: ~a~n" arg val))
|
(printf "\t~a: ~a~n" arg val))
|
||||||
(let ([e (apply f l)])
|
(let ([e (with-handlers ([values (lambda (exn)
|
||||||
(printf "result was ~a~n" e)
|
(printf "~a raised exception ~a~n" 'f exn)
|
||||||
|
(raise exn))])
|
||||||
|
(apply f l))])
|
||||||
|
(printf "~a result was ~a~n" 'f e)
|
||||||
e)))]
|
e)))]
|
||||||
[(_ f . args) (debug (f . args))]))
|
[(_ f . args) (debug (f . args))]))
|
|
@ -1,6 +1,7 @@
|
||||||
#lang scheme/base
|
#lang scheme/base
|
||||||
(require scheme/contract
|
(require scheme/contract
|
||||||
scheme/dict)
|
scheme/dict
|
||||||
|
(for-syntax scheme/base))
|
||||||
|
|
||||||
; list-prefix : list? list? -> boolean?
|
; list-prefix : list? list? -> boolean?
|
||||||
; Is l a prefix or r?
|
; Is l a prefix or r?
|
||||||
|
@ -84,3 +85,30 @@
|
||||||
(same? key-item prev))
|
(same? key-item prev))
|
||||||
(car items)
|
(car items)
|
||||||
(loop (cdr items) (cons key-item sofar)))))))
|
(loop (cdr items) (cons key-item sofar)))))))
|
||||||
|
|
||||||
|
;; sam added from carl
|
||||||
|
|
||||||
|
(define-syntax (values->list stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(vl expr)
|
||||||
|
(syntax/loc stx
|
||||||
|
(call-with-values (lambda () expr) list))]))
|
||||||
|
|
||||||
|
(define (map/list n f ls)
|
||||||
|
(cond
|
||||||
|
[(andmap null? ls) (build-list n (lambda (i) null))]
|
||||||
|
[(andmap pair? ls)
|
||||||
|
(let* ([vs (values->list (apply f (map car ls)))]
|
||||||
|
[k (length vs)])
|
||||||
|
(unless (= k n)
|
||||||
|
(error 'map/values
|
||||||
|
"~a produced ~a values, not ~a: ~e"
|
||||||
|
f k n vs))
|
||||||
|
(map cons vs (map/list n f (map cdr ls))))]
|
||||||
|
[else (error 'map/values "list lengths differ")]))
|
||||||
|
|
||||||
|
(define (map/values n f . ls)
|
||||||
|
(apply values (map/list n f ls)))
|
||||||
|
|
||||||
|
(provide map/values)
|
||||||
|
|
||||||
|
|
|
@ -79,3 +79,27 @@ true value. The procedures @scheme[equal?], @scheme[eqv?], and
|
||||||
(dict-map id-t list)
|
(dict-map id-t list)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@addition{Carl Eastlund}
|
||||||
|
|
||||||
|
@defproc[(map/values [n natural-number/c]
|
||||||
|
[f (-> A ... (values B_1 ... B_n))]
|
||||||
|
[lst (listof A)]
|
||||||
|
...)
|
||||||
|
(values (listof B_1) ... (listof B_n))]{
|
||||||
|
|
||||||
|
Produces lists of the respective values of @scheme[f] applied to the elements in
|
||||||
|
@scheme[lst ...] sequentially.
|
||||||
|
|
||||||
|
@defexamples[
|
||||||
|
#:eval the-eval
|
||||||
|
(map/values
|
||||||
|
3
|
||||||
|
(lambda (x)
|
||||||
|
(values (+ x 1) x (- x 1)))
|
||||||
|
(list 1 2 3))
|
||||||
|
]
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user