public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: Bent Phillipsen <bent.phillipsen@gmail.com>
Cc: Kawa mailing list <kawa@sourceware.org>,
	bigloo@sophia.inria.fr, 	Per Bothner <per@bothner.com>
Subject: Re: [bigloo] behavior of CASE with strings
Date: Sat, 21 Jan 2017 10:07:00 -0000	[thread overview]
Message-ID: <CADEOadf+++LDz8srLL2p3Vh6hgpjMiqF2-g1-O2_7K9bp1eGEw@mail.gmail.com> (raw)
In-Reply-To: <CAD1yAURxqeVTmpbktQr-+F6hKZUySg6zQbRzjB224=wTvfNa-w@mail.gmail.com>

Hi bent,

i did not completely understood your example, now it's clear it solves
the problem,
with 'member' instead of 'memv' it's ok for the strings.

i will use this next week,

Thanks a lot,

Damien

On Tue, Jan 17, 2017 at 8:27 PM, Bent Phillipsen
<bent.phillipsen@gmail.com> wrote:
> Hi Damien,
>
> This question has already been answered. Should it be of interest, I would
> like to add the following:
>
> R5RS specifies that the case expression tests equivalence with eqv? (p.10),
> but also that equivalence test of strings with eqv? is 'unspecified' (p.18).
> This has the consequence that stings cannot be used as key and clause in
> case expressions in a well defined way (or at least portable way). You can
> however easily define a macro that *also* can use strings. You can write
> your own; but simply taking the case macro defined in R5RS (p.43) ensures
> that all corner cases are handled correctly. You then just need to replace
> 'memv' with 'member' (this has the effect of testing equivalence with equal?
> instead of eqv?). You probably want to change the name of the macro also,
> for instance to case-member, to avoid shadowing the original case
> expression. You will get something like the following, which should work and
> which I think is portable. (There is of cause a (mostly insignificant)
> performance penalty in testing with equal? instead of eqv?):
>
> --------------------------------------
>
> (module test-case-member
>         (main start))
>
> (define-syntax case-member
>         (syntax-rules (else)
>                 ((case-member (key ...)
>                         clauses ...)
>                  (let ((atom-key (key ...)))
>                         (case-member atom-key clauses ...)))
>                 ((case-member key
>                         (else result1 result2 ...))
>                  (begin result1 result2 ...))
>                 ((case-member key
>                         ((atoms ...) result1 result2 ...))
>                  (if (member key '(atoms ...))
>                         (begin result1 result2 ...)))
>                 ((case-member key
>                         ((atoms ...) result1 result2 ...) clause clauses
> ...)
>                  (if (member key '(atoms ...))
>                         (begin result1 result2 ...)
>                         (case-member key clause clauses ...)))))
>
> (define (start argv)
>
>         (define x (string-append "b" "a" "r"))
>         (define y "animal")
>         (define z 'composit)
>
>         (print "next line should be '" y "':")
>         (print (case-member (string-append "do" "g") (("cat" "dog" "mouse")
> y) (else "mineral or vegetable")))
>
>         (print (string-append "next line should be '" x "':"))
>         (case-member x
>                 (("foo")  (print 'foo))
>                 (("bar")  (print 'bar))
>                 (else "it was not foo or bar"))
>
>         (print (string-append "next line should be '" (symbol->string z)
> "':"))
>         (print (case-member (* 2 3) ((2 3 5 7) 'prime)
>                                     ((1 4 6 8 9) z))))
>
> ---------------------------------
>
> Best regards
>
> Bent
>
>
> 2017-01-16 14:32 GMT+01:00 <Manuel.Serrano@inria.fr>:
>>
>> Hi Damien,
>>
>> Bigloo complies with the R5RS specification (see section 4.2) that says:
>>
>>
>> -----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----
>> case <key> <clause1> <clause2> ...,     library syntax
>> Syntax: <Key> may be any expression. Each <clause> should have the form
>>
>> ((<datum1> ...,) <expression1> <expression2> ...,),
>>
>> where each <datum> is an external representation of some object. All the
>> <datum>s must be distinct. The last <clause> may be an ``else clause,''
>> which has the form
>>
>> (else <expression1> <expression2> ...,).
>>
>> Semantics: A case expression is evaluated as follows. <Key> is evaluated
>> and its result is compared against each <datum>. If the result of evaluating
>> <key> is equivalent (in the sense of eqv?; see section Equivalence
>> predicates) to a <datum>, then the expressions in the corresponding <clause>
>> are evaluated from left to right and the result(s) of the last expression in
>> the <clause> is(are) returned as the result(s) of the case expression. If
>> the result of evaluating <key> is different from every <datum>, then if
>> there is an else clause its expressions are evaluated and the result(s) of
>> the last is(are) the result(s) of the case expression; otherwise the result
>> of the case expression is unspecified.
>>
>> (case (* 2 3)
>>   ((2 3 5 7) 'prime)
>>   ((1 4 6 8 9) 'composite))            =>  composite
>> (case (car '(c d))
>>   ((a) 'a)
>>   ((b) 'b))                            =>  unspecified
>> (case (car '(c d))
>>   ((a e i o u) 'vowel)
>>   ((w y) 'semivowel)
>>   (else 'consonant))                   =>  consonant
>>
>> -----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----
>>
>> You can observe that with:
>>
>> (pp (expand '(case x (("foo") 1) (("bar" ) 2))))
>> =>
>> (let ((case-value x))
>>   (if (eqv? case-value '"foo")
>>     1
>>     (if (eqv? case-value '"bar") 2 #unspecified)))
>>
>> Cheers,
>>
>> --
>> Manuel
>
>

  parent reply	other threads:[~2017-01-21 10:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <201701121129.54147.Damien.Mattei@unice.fr>
2017-01-16 13:34 ` Manuel.Serrano
     [not found]   ` <CAD1yAURxqeVTmpbktQr-+F6hKZUySg6zQbRzjB224=wTvfNa-w@mail.gmail.com>
2017-01-21 10:07     ` Damien Mattei [this message]
2017-01-21 16:55       ` Per Bothner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADEOadf+++LDz8srLL2p3Vh6hgpjMiqF2-g1-O2_7K9bp1eGEw@mail.gmail.com \
    --to=damien.mattei@gmail.com \
    --cc=bent.phillipsen@gmail.com \
    --cc=bigloo@sophia.inria.fr \
    --cc=kawa@sourceware.org \
    --cc=per@bothner.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).