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

Re: New release of SRFI 114 with implementation



On 12/08/2013 09:01 AM, John Cowan wrote:
> Kevin Wortman scripsit:
> 
>> Returning the first tied argument seems like it would be a little more
>> straightforward to implement, at least to me. But I'm comfortable with
>> the current behavior. I don't think it matters much as long as the
>> behavior is documented.
> 
> Here's the current sample implementation (note that it depends on `apply`
> calling its argument tail-recursively):
> 
> (define comparator-min
>   (case-lambda
>     ((comparator a)
>       a)
>     ((comparator a b)
>      (if (<? comparator a b) a b))
>     ((comparator a b . objs)
>      (comparator-min comparator a (apply comparator-min comparator b objs)))))
> 
> (define comparator-max
>   (case-lambda
>     ((comparator a)
>       a)
>     ((comparator a b)
>      (if (>? comparator a b) a b))
>     ((comparator a b . objs)
>      (comparator-max comparator a (apply comparator-max comparator b objs)))))
> 
> Exactly what you get depends on which predicate you use, of course.

Gotcha. I was thinking of the algorithm: scan each element and keep
track of the smallest (resp. largest) element you've seen so far.
Something like (untested):

(define (comparator-min comparator first . rest)
  (fold (lambda (x least)
          (if (<? comparator x least) x least))
        first
        rest))

Kevin Wortman