unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Keith Wright <kwright@keithdiane.us>
To: Zelphir Kaltstahl <zelphirkaltstahl@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: guile-user Digest, Vol 188, Issue 9
Date: Sun, 08 Jul 2018 23:42:31 -0400	[thread overview]
Message-ID: <87tvp96pnc.fsf@fcs20.keithdiane.us> (raw)
In-Reply-To: <349a3f85-4bae-3a35-4a1f-08cd6385255b@gmail.com> (message from Zelphir Kaltstahl on Sun, 8 Jul 2018 19:00:40 +0200)

Zelphir Kaltstahl <zelphirkaltstahl@gmail.com> writes:

>>> I decided to take a look at how one can parse command line arguments in
>>> Guile and was looking for something like argparse in Python. It seems
>>> that (use-modules (ice-9 getopt-long)) does the job, except that I hit
>>> one problem and don't know what the mistake I am making is. It seems to
>>> be connected to the usage of `predicate` in my code.
>>>
>>>;; ===== EXAMPLE START =====
>>>(define (string-exact-integer? str)
>>>  (exact-integer? (string->number str)))
>>>
>>>(define option-spec
>>>  '((version ... (predicate string-exact-integer?))))
>>>
>>> I am providing a function that takes the
>>> option's value as string and returns #t or #f.

>> From: Matt Wette <matt.wette@gmail.com>
>>
>> You probably want to use quasi-quote + unquote:
>>  ? `((version ... (predicate ,string-exact-integer?))))

Zelphir Kaltstahl <zelphirkaltstahl@gmail.com> writes:
>
> That solved the problem, thank you Matt!
> I was so far quite fond of the way one specifies options with
> getopt-long, but the quasi-quote unquote was not mentioned in the Guile
> docs and feels unnatural.

Actually my copy of the manual says:
  (predicate func)
    ...
     then getopt-long will apply func to the value,
     and throw an exception if it returns #f.

    func should be a procedure which accepts a string and
    returns a boolean value; you may need to use
    quasiquotes to get it intoa grammar.

So a warning about quasiquote _is_ mentioned in the Guile docs,
although it wouldn't hurt to put in an example to clarify just _how_
to "use quasiquote to get it into the grammmar."

> There seems to be no reason for it to force me
> to do that, except that it does not work otherwise.

When I first read that, I was exasperated.  What more reason
do you need to do it right?  It works that way and doesn't
work if you do it wrong.

Upon further reflection I realized that you are probably not
asking why right is better than wrong, but confused about
what quasiquote does.

The manual says:
  func should be a procedure

You said:
>>> I am providing a function that takes the
>>> option's value as string and returns #t or #f.

but that is _not_ what you were doing.  If the whole
list is quoted, then the occurrence of "string-exact-integer?"
in the list is a _symbol_.  It must be unquoted so
that it will be evaluated to a function (i.e. procedure).

It's like the difference between (+ 2 2) and ('+ 2 2).

The manual could use more explanation, but I believe
it is correct.

Hope this helps.

  -- Keith



  parent reply	other threads:[~2018-07-09  3:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.77.1531065622.9222.guile-user@gnu.org>
2018-07-08 17:00 ` guile-user Digest, Vol 188, Issue 9 Zelphir Kaltstahl
2018-07-08 17:10   ` Matt Wette
2018-07-09  3:42   ` Keith Wright [this message]
2018-07-09  6:24     ` Zelphir Kaltstahl
2018-07-10  9:50   ` Alex Sassmannshausen
2018-07-10 20:10     ` Guile Config Zelphir Kaltstahl
2018-07-11 13:33       ` Alex Sassmannshausen
2018-07-15 10:47         ` Zelphir Kaltstahl

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

  List information: https://www.gnu.org/software/guile/

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

  git send-email \
    --in-reply-to=87tvp96pnc.fsf@fcs20.keithdiane.us \
    --to=kwright@keithdiane.us \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@gmail.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).