guile-user
[Top][All Lists]
Advanced

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

Re: Bug in and-map and or-map? What am I doing wrong?


From: Alejandro Forero Cuervo
Subject: Re: Bug in and-map and or-map? What am I doing wrong?
Date: Wed, 9 May 2001 18:51:25 -0500
User-agent: Mutt/1.2.5i

    However, rather than the definitions you sent me (and, by the way,
    thank you very much for your prompt reply), I would use the following
    two:

    <snip>

Oh darn, I just found a problem on them:

       (and-map equal? (list 1) (list 2 3))
    => #f

I think they should signal an error since the lists have different
lengths.  I am an idiot.

I am now proposing two better versions that do detect this problem and
I still think work faster than the versions you proposed:

(define (and-map f . lsts)
  (define (badlength) (error "Lists have different lengths"))
  (let loop ((l lsts))
     (if (null? (car l))
         (if (or (null? (cdr l)) (and-map null? (cdr l))) #t (badlength))
         (if (apply f (map (lambda (c) (if (null? c) (badlength) (car c))) l))
             (loop (map cdr l))
             (if (apply eq? (map length l)) #f (badlength))))))

(define (or-map f . lsts)
  (define (badlength) (error "Lists have different lengths"))
  (let loop ((l lsts))
     (if (null? (car l))
         (if (or (null? (cdr l)) (and-map null? (cdr l))) #f (badlength))
         (if (apply f (map (lambda (c) (if (null? c) (badlength) (car c))) l))
             (if (apply eq? (map length l)) #t (badlength))
             (loop (map cdr l))))))

I am sorry for all this noise, I know the speed diferences between
your versions and these are insignificant... :(

Alejo.
http://bachue.com/alejo

--
The mere formulation of a problem is far more essential than its solution.
      -- Albert Einstein.

$0='!/sfldbi!yjoV0msfQ!sfiupob!utvK'x44;print map{("\e[7m \e[0m",chr ord
(chop$0)-1)[$_].("\n")[++$i%77]}split//,unpack'B*',pack'H*',($F='F'x19).
"F0F3E0607879CC1E0F0F339F3FF399C666733333CCF87F99E6133999999E67CFFCCF3".
"219CC1CCC033E7E660198CCE4E66798303873CCE60F3387$F"#Don't you love Perl?

Attachment: pgpYEP4mKOmD9.pgp
Description: PGP signature


reply via email to

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