[racket] (values) vs (void)
On Nov 24, 2014, at 8:49 AM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> Like this?
>
> (and
> (> y 0)
> (set! x (> y 3))
> (set! y (- y 1)))
>
> I thought, that (when ...) should be used in such case.
> (and ..) instead will be rather confusing (for example, Common Lispers expect, that (set! x #f) is #f).
Well since in racket it returns void which is a truthy value, in racket you can use and just like begin except that if one of the expressions returns #f it stops. Also you can use (or (and condition then-expr ...) …) as a kind of variant of cond that uses #f to signal a fall-through to the next “clause”.
I find these kinds of things pretty convenient and intuitive, once I got used to void being a truthy value and and and or returning non-boolean answers.
And also, there won’t be a lot of situations where the context expects zero values, and for the one you showed, you can do this:
(define-syntax-rule (local-phase-1 expr …)
(define-synatxes ()
(let () expr ... (values))))
And if you want to use it within something like (let-syntaxes ([() (something)]) …),
(define-simple-macro (my-let-syntaxes (binding …) body:expr …+)
#:with (new-binding …)
(for/list ([binding (in-list (syntax->list #’(binding …)))])
(syntax-parse binding
[[(id …) expr …] #’[(id …) (let () expr …)]]
[[(~literal _) expr …] #’[() (let () expr … (values))]]))
#’(let-syntaxes (new-binding …) body …))
>
>
> Sun, 23 Nov 2014 22:47:23 -0500 от "Alexander D. Knauth" <alexander at knauth.org>:
> Most contexts expect one value, and you can easily define a macro that does the
> (define-syntaxes ()
> (begin0 (values) expr …))
> or
> (define-syntaxes ()
> (begin expr … (values)))
> for you.
>
> The one that comes too my mind first is
> (and (test-something-about x) (do-something-with x) (do-something-else-with x)),
> where (do-something-with x) just returns void (which is a truthy value), but only if the test is true.
>
> On Nov 23, 2014, at 9:17 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
>
>> I can't imagine an example, where one need to use void function in context, that expects a single value.
>> Moreover, in macro definition I had to write
>>
>> (define-syntaxes ()
>> (begin0 (values)
>> (save-fields! #'NAME (list 'ALL-FIELD ...))
>>
>> to make side effect work in expansion time. I cannot write simply (save-fields! #'NAME (list 'ALL-FIELD ...), because save-fields! is at phase-1.
>> I prefer to write simply
>>
>> (define-syntaxes () (save-fields! #'NAME (list 'ALL-FIELD ...))
>>
>> but then save-fields! should return no values, not one void value.
>>
>> Can you bring an example, where one have to to use void function in a context that expects a single value?
>>
>>
>> Sun, 23 Nov 2014 18:21:47 -0500 от "Alexander D. Knauth" <alexander at knauth.org>:
>>
>> On Nov 23, 2014, at 1:31 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
>>
>>> > void is a good placeholder in dummy functions/objects
>>>
>>> (define (foo) (values)) also works just fine.
>>
>> But then you can’t use (foo) in a context that expects a single value, but if you use (void) instead of (values), it will return one value (instead of zero values), and so you can use it in contexts that expect one value.
>>
>>>
>>> > fill a gap, which is useful
>>>
>>> If function returns void?, then it is used as a procedure. I mean returned value never assigned to a variable.
>>> So in what cases it is useful?
>>>
>>>
>>> Sun, 23 Nov 2014 13:20:26 +0000 от Stephen De Gabrielle <spdegabrielle at gmail.com>:
>>> void is a good placeholder in dummy functions/objects. It doesn't do anything, except for fill a gap, which is useful.
>>>
>>> Values is used to pass multiple values.
>>>
>>> Check the manual for details.
>>>
>>> Does that help?
>>>
>>> Stephen
>>>
>>> On Sun, 23 Nov 2014 at 12:56 Roman Klochkov <kalimehtar at mail.ru> wrote:
>>> When I should use (void) and when (values)?
>>>
>>> They are both used to show, that there are no return values.
>>> They are both not printable.
>>>
>>> What is intended use for (void) and (values) and when one should prefer one over other?
>>>
>>> --
>>> Roman Klochkov
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users
>>>
>>>
>>> --
>>> Roman Klochkov
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users
>>
>>
>>
>> --
>> Roman Klochkov
>
>
>
> --
> Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141124/7a322d0f/attachment.html>