[racket] Is there a better way to do this in Racket?
Thanks Eli,
This is what I was looking for, something short and crystal clear.
I've shortened it even more. My data is actually a list of lists
which I had converted to a vector of vectors, so I've deferred that
for now. I've also used Matthias' macro of let** he shared the other
day so now I've got:
(define (list-of-ranges-of-ones lst)
(let** t (map (λ (x) (integer->char (+ 48 x))) lst)
(list->string t)
(regexp-match-positions* #rx"1+" t)
(map (λ (x) (list (car x) (sub1 (cdr x)))) t)))
I had vaguely thought while working on this, "wouldn't it be nice if I
could just use regular expressions", but I didn't make the leap of
imagination to simply convert the vector into a string, because I
never assumed it would be so simple. i.e. "list->string". Racket
seems to reward the imaginative.
One other thing. I tried to substitute "first" and "rest" for car and
cdr in your example, till I saw that these synonyms can only be used
on proper lists. I'm wondering if that restriction could be removed
in the language so they are true synonyms for car and cdr.
Cheers,
Harry
On Sun, May 13, 2012 at 5:52 PM, Eli Barzilay <eli at barzilay.org> wrote:
> Just now, Harry Spier wrote:
>> Thanks Neil,
>>
>> Each vector is in the order of about a thousand cells and there are
>> about a thousand of these vectors that are processed at one time.
>> So as long as I can do that in 10 or 20 seconds thats fine. So as
>> long as its "relatively efficient" I think I'm OK. Whats just as
>> important to me, is "clarity of the code", so that when I go back
>> and look at this and other code six months from now I can still
>> figure out what I did.
>
> (These are relatively small numbers...)
>
> In any case, here's a solution that is not meant to be used as-is, but
> it makes a good point to consider the nature of your problem... (If
> efficiency does become an issue, then it would be better to work with
> bytestrings instead of vectors anyway.)
>
> (define (list-of-ranges-of-ones v)
> (let* ([t (vector->list v)]
> [t (map (λ (x) (integer->char (+ 32 (* x 17)))) t)]
> [t (list->string t)]
> [t (regexp-match-positions* #rx"1+" t)]
> [t (map (λ (x) (list (car x) (sub1 (cdr x)))) t)])
> t))
>
> --
> ((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
> http://barzilay.org/ Maze is Life!