Welcome to the CHICKEN Scheme pasting service

anagram-index added by Tobia on Wed Jun 8 20:11:19 2011

(use srfi-1)

(define (position item lst) ;why is this not in srfi-1
  (list-index (lambda (x) (equal? x item)) lst))

(define (permutation-index permuted original)
  (let loop ((sum 0)
             (perm permuted)
             (orig original)
             (radix (length permuted)))
    (if (null? perm)
        sum
        (let ((elem (car perm)))
          (loop (+ (* radix sum)
                   (position elem orig))
                (cdr perm)
                (delete elem orig)
                (- radix 1))))))

(define (anagram-index anagram)
  (let ((original (sort anagram <)))
    (permutation-index anagram original)))

(anagram-index '(1 2 3)) ;=> 0
(anagram-index '(1 3 2)) ;=> 1
(anagram-index '(2 1 3)) ;=> 2
(anagram-index '(2 3 1)) ;=> 3
(anagram-index '(3 1 2)) ;=> 4
(anagram-index '(3 2 1)) ;=> 5

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
What's the Makefile target to generate the bootstrap compiler of CHICKEN?
Visually impaired? Let me spell it for you (wav file) download WAV