lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scheme question: convert a range


From: David Nalesnik
Subject: Re: Scheme question: convert a range
Date: Mon, 16 Nov 2015 17:24:46 -0600

Hi Simon,

On Sun, Nov 15, 2015 at 12:53 PM, Simon Albrecht <address@hidden> wrote:
Hello,

The subject certainly seems cryptic – it’s difficult to summarize, but an example will make it clear immediately.
I want to write a scheme procedure, which takes a pair like #'(3 . 7) and returns a list with all the numbers in the range: #'(3 4 5 6 7)
How is this done most easily?


Well, if the numbers are ascending you could do something like:

(define (expand-interval arg)
   (iota (- (1+ (cdr arg)) (car arg)) (car arg)))

(display (expand-interval '(3 . 7)))

If not, something like this would work:

(define (expand-interval arg)
   (let ((dir (if (> (car arg) (cdr arg)) -1 1)))
     (let loop ((elt (car arg)) (result '()))
       (if (= elt (+ dir (cdr arg)))
           (reverse result)
           (loop (+ dir elt) (cons elt result))))))

HTH,
David


reply via email to

[Prev in Thread] Current Thread [Next in Thread]