Re: Usecase: chaining operations after "optionals"
Arne Babenhauserheide 10 Jun 2020 08:03 UTC
Marc Nieper-Wißkirchen <xxxxxx@nieper-wisskirchen.de> writes:
> Am Mi., 10. Juni 2020 um 00:08 Uhr schrieb Arne Babenhauserheide <
> xxxxxx@web.de>:
>> > Personally, I would choose `and-let*', which works in all cases
>> > (regardless of the position of the arguments).
>>
>> and-let* looks unexpectedly nice, yes. It is much longer, but explicit.
>> For my most common case (need cdr after assoc), the difference is
>> significantly in favor of the new syntax, though.
>
> If you need `cdr' after `assoc' very often, I'd use a helper procedure like
> this one:
>
> (define (assoc-ref key alist)
> (and-let* ((entry (assoc key alist)))
> (cdr entry)))
>
> This way, you don't have any magic `cdr's in your code and you express your
> intent as clearly as possible. (And choose whatever style you want in the
> body of `assoc-ref'. :))
This breaks the correctnsess of assoc: That #f is always unambigous as
"not found". That’s why I want to preserve the behaviour and only unpack
in the final processing.
Best wishes,
Arne
--
Unpolitisch sein
heißt politisch sein
ohne es zu merken