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

About this user

Sumimus L

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

mapi: similar to standard map in Scheme, but function has index of elements as second parameter.


;;;  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

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS