[racket] member et al.

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Thu Nov 11 12:56:17 EST 2010

On Thu, Nov 11, 2010 at 10:31 AM, Neil Toronto <neil.toronto at gmail.com> wrote:
> Eli Barzilay wrote:
>>
>> Four minutes ago, Neil Toronto wrote:
>>>
>>> I think we should retire this truthiness business, replace cond's
>>> `=>' with something that lets you easily nest conds (I have a
>>> proposal ready for this, and it is awesome),
>>
>> (a) how is this related?  (b) `=>' is not used to nest `cond's.
>
> You use it to have the same computed value available in both the test-expr
> and the then-body, which would otherwise require nested `cond's.
>
> I got inspired by the #:when form in `for' loops that flattens nesting, and
> wrote a `cond*' macro that allows this instead:
>
> (cond* [... some stuff ...]
>       #:with (define m ...)
>       [(... something about m ...)  (... something using m ...)]
>       ... more things referring to m ...)
>
> It's more general, since it doesn't require encoding the condition you want
> to test as a truthiness value. Also, the `define' doesn't have to be a
> `define' - it can be any legal expression.
>
> It's flattened most of my numeric code. Numeric code often computes and
> reacts to easily computable, approximate conditions first. When the easy
> tests are inconclusive, it computes and reacts to progressively more precise
> conditions that are more expensive.
>
>>> and stop using truthiness idioms. It's obfuscating, it's hard on
>>> newcomers, it makes reasoning about return values difficult, and
>>> it's very last-century.
>>
>> (And tail-call-{optimization,elimination} is very this-century...)
>
> I've never heard this, and I am curious. How does truthiness in `and' and
> `or' preserve proper tail calls?

(define (andmap f l)
 (if (empty? l)
     #t
     (and (f (first l)) (andmap f (rest l)))))

If and has to return a boolean, then it leaves space on the stack to
check if andmap returns a bool or to convert "truth" to #t. Thus this
program goes from constant stack space to linear stack space.

Jay

>
> Neil T
>
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.