Re: Is DSSSL Syntax Tricky?

Subject: Re: Is DSSSL Syntax Tricky?
From: James Clark <jjc@xxxxxxxxxx>
Date: Wed, 21 May 1997 10:36:30 +0700
At 14:30 20/05/97 -0400, Paul Prescod wrote:
>I just realized that it isn't really fair to ask if people were familiar with 
>the map idiom because map isn't in Jade. You would have had to already
>understand recursion and higher order functions to implement it. Here's the
>implementation for those who want to try to start using it:
>
>(define (map func lst)
>    (let loop((lst lst))
>	(if (null? lst) '()
>	    (cons (func (car lst)) (loop (cdr lst))))))

That's an odd way to define it.  If you don't want a tail-recursive
implementation you can just do:

(define (map func lst)
  (if (null? lst)
      '()
      (cons (func (car lst)) (map func (cdr lst)))))

If you do want a tail-recursive implementation, then you need something like
this:

(define (map func lst)
  (let loop ((lst lst)
             (result '()))
    (if (null? lst)
        (reverse result)
        (loop (cdr lst) (cons (func (car lst)) result)))))

Actually neither of these are complete implementations since you can have a
func can take more than one argument in which case map has additional
argument lists, ie

(map + '(1 2 3) '( 1 2 3)) will return (2 4 6)

>Please consider this a feature request, James. =)

Not high on my list since you can implement yourself easily.

James


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


Current Thread