Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

mapi: similar to standard map in Scheme, but function has index of elements as second parameter. (See related posts)


;;;  mapi: similar to standard map in Scheme, but function has index of elements as parameter.
;;;
(define (mapi p l)
  (let loop ((l l) (i 0) (r '()))
    (if (pair? l)
	(loop (cdr l) (+ i 1) (cons (p (car l) i) r))
	(reverse r))))

;; Example: converting a binary number given as a list of binary digits. 
;; (apply + (mapi (lambda (x i) (* x (expt 2 i))) '(0 1 1)))
;; => 6


You need to create an account or log in to post comments to this site.


Click here to browse all 4857 code snippets

Related Posts