guile-user
[Top][All Lists]
Advanced

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

Re: guile-user Digest, Vol 188, Issue 9


From: Keith Wright
Subject: Re: guile-user Digest, Vol 188, Issue 9
Date: Sun, 08 Jul 2018 23:42:31 -0400

Zelphir Kaltstahl <address@hidden> 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 <address@hidden>
>>
>> You probably want to use quasi-quote + unquote:
>>  ? `((version ... (predicate ,string-exact-integer?))))

Zelphir Kaltstahl <address@hidden> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]