[plt-scheme] Is there an idiom for throwing away some values?

From: Eric Hanchrow (eric.hanchrow at gmail.com)
Date: Tue Jul 28 00:09:21 EDT 2009

Here's some code that approximates e^x:

(define (series x)
  (call-with-values
      (lambda ()
        (for/fold ([sum 0]
                   [factor 1])
            ([n (in-range 20)])
            (values
             (+ sum factor)
             (* factor (/ x (add1 n))))))
    (lambda (sum ignore-me)
      sum)))

I'd like to be able to instead do something like

(define (series x)
  (just-the-first-value
   (for/fold ([sum 0]
              [factor 1])
       ([n (in-range 20)])
       (values
        (+ sum factor)
        (* factor (/ x (add1 n)))))))

... without having to actually write "just-the-first-value" myself :)

Is there an idiom that makes this simpler than what I was doing above?


Posted on the users mailing list.