Re: Iteration with named let

Subject: Re: Iteration with named let
From: "Henry S. Thompson" <ht@xxxxxxxxxxxxxxx>
Date: Wed, 25 Jun 97 10:13:32 BST
>  Here's a function I created to determine if a child has a particular GI:
>  
>  (define (has-child? %gi% %element%)
>    (let loop ((desc-list (children %element%)))
>      (if (equal? (gi (node-list-first desc-list)) %gi%)
>         #t
>         (if (node-list-empty? desc-list)
>            #f
>            (loop (node-list-rest desc-list))
>        )
>      )
>    )
>  )

People have already replied about a one-line way to do this.

There is at least an infelicity in your style, at worst a bug, which
given your gracious willingness to be coached I'll tackle -- usually
in let loops you do the loop check at the top:

(let loop ((desc-list (children %element%)))
     (if (node-list-empty? desc-list)
	 #f
       (or (equal? (gi (node-list-first desc-list))
		   %gi%)
	   (loop (node-list-rest desc-list)))))

Your code as written asks for the gi of the empty-node-list in any
non-matching case, including the case where %element% had no children,
which probably won't cause an error, but only for pretty subtle
reasons, whereas the above recasting of it doesn't do that, and will
will handle the no-children case cleanly.

ht


 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread