简体   繁体   中英

accumulate not working in Scheme Lang R5RS

I solved a problem which involved using map and accumulate but it gave me this error: "accumulate: undefined;" cannot reference undefined identifier. "accumulate: undefined;" cannot reference undefined identifier.

This is my code:

;Procedure to test it with
(define (double x)
   (* 2 x))

(define (my-map proc sequence) 
   (accumulate (lambda (x y) (cons (proc x) y)) '() sequence))

Please can anyone tell me how to fix this problem?

The procedure accumulate is not defined in R5RS. But it's simple enough to define your own:

(define (accumulate proc init lst)
  (if (null? lst)
      init
      (proc (car lst)
            (accumulate proc init (cdr lst)))))

For future reference, accumulate is also known as foldr , fold-right , inject or reduce in other programming languages, consult your interpreter's documentation for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM