unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* ?-suffix for booleans... good-idea? or bad-idea?
@ 2017-04-24 17:01 Christopher Allan Webber
  2017-04-25  3:43 ` Alex Vong
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Christopher Allan Webber @ 2017-04-24 17:01 UTC (permalink / raw)
  To: guile-user@gnu.org

Hello everyone!  Here's a little bikeshed for us to paint.

I've noticed that it's common in Guile modules to use "foo?" for
variable names involving booleans.  It's tempting, because this looks
an awful lot like you're asking a question... and it's also common
for this even to be keyword arguments to procedures, etc.

But is it a good idea?  I thought "foo?" was supposed to be for
predicates, as a nicer version of the "foo-p" predicate convention in
other non-scheme lisps.  I can't imagine other lisps doing "foo-p" for
just variables with boolean values.

On the other hand, once you start adding ? to the end of boolean'y
things, it *does* become tempting to put them at the end of boolean
variables and arguments.  It looks pretty nice.

What do people think?  I'm struggling with deciding what's the right
thing for my own code, but leaning towards "we shouldn't use the ?
suffix for just boolean values".

 - Chris



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
@ 2017-04-25  3:43 ` Alex Vong
  2017-04-25 15:50   ` Jan Wedekind
  2017-04-25 18:08   ` Neil Jerram
  2017-04-25 16:53 ` Panicz Maciej Godek
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 13+ messages in thread
From: Alex Vong @ 2017-04-25  3:43 UTC (permalink / raw)
  To: Christopher Allan Webber; +Cc: guile-user@gnu.org

[-- Attachment #1: Type: text/plain, Size: 1362 bytes --]

Good question! I can't decide as well. I want to know how people think
about it.

In a lazy language, a variable is a 0-ary thunk, while a predicate is a
1-ary thunk. Since they are really just special case of a general thing,
it make sense to use foo? for both cases. But we all know guile is not
lazy, so I really don't know what to do.

Christopher Allan Webber <cwebber@dustycloud.org> writes:

> Hello everyone!  Here's a little bikeshed for us to paint.
>
> I've noticed that it's common in Guile modules to use "foo?" for
> variable names involving booleans.  It's tempting, because this looks
> an awful lot like you're asking a question... and it's also common
> for this even to be keyword arguments to procedures, etc.
>
> But is it a good idea?  I thought "foo?" was supposed to be for
> predicates, as a nicer version of the "foo-p" predicate convention in
> other non-scheme lisps.  I can't imagine other lisps doing "foo-p" for
> just variables with boolean values.
>
> On the other hand, once you start adding ? to the end of boolean'y
> things, it *does* become tempting to put them at the end of boolean
> variables and arguments.  It looks pretty nice.
>
> What do people think?  I'm struggling with deciding what's the right
> thing for my own code, but leaning towards "we shouldn't use the ?
> suffix for just boolean values".
>
>  - Chris

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-25  3:43 ` Alex Vong
@ 2017-04-25 15:50   ` Jan Wedekind
  2017-04-25 16:21     ` Paul
  2017-04-25 18:08   ` Neil Jerram
  1 sibling, 1 reply; 13+ messages in thread
From: Jan Wedekind @ 2017-04-25 15:50 UTC (permalink / raw)
  To: Alex Vong, Christopher Allan Webber; +Cc: guile-user@gnu.org

In Ruby ...? is used for methods returning boolean. E.g. 5.even?. Boolean variables are not sufficed. E.g. x = 5.even? The method call "even?" is the question. "x" is the answer.

On 25. April 2017 04:43:23 GMT+00:00, Alex Vong <alexvong1995@gmail.com> wrote:
>Good question! I can't decide as well. I want to know how people think
>about it.
>
>In a lazy language, a variable is a 0-ary thunk, while a predicate is a
>1-ary thunk. Since they are really just special case of a general
>thing,
>it make sense to use foo? for both cases. But we all know guile is not
>lazy, so I really don't know what to do.
>
>Christopher Allan Webber <cwebber@dustycloud.org> writes:
>
>> Hello everyone!  Here's a little bikeshed for us to paint.
>>
>> I've noticed that it's common in Guile modules to use "foo?" for
>> variable names involving booleans.  It's tempting, because this looks
>> an awful lot like you're asking a question... and it's also common
>> for this even to be keyword arguments to procedures, etc.
>>
>> But is it a good idea?  I thought "foo?" was supposed to be for
>> predicates, as a nicer version of the "foo-p" predicate convention in
>> other non-scheme lisps.  I can't imagine other lisps doing "foo-p"
>for
>> just variables with boolean values.
>>
>> On the other hand, once you start adding ? to the end of boolean'y
>> things, it *does* become tempting to put them at the end of boolean
>> variables and arguments.  It looks pretty nice.
>>
>> What do people think?  I'm struggling with deciding what's the right
>> thing for my own code, but leaning towards "we shouldn't use the ?
>> suffix for just boolean values".
>>
>>  - Chris

-- 
Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-25 15:50   ` Jan Wedekind
@ 2017-04-25 16:21     ` Paul
  2017-04-26 15:25       ` Alex Kost
  0 siblings, 1 reply; 13+ messages in thread
From: Paul @ 2017-04-25 16:21 UTC (permalink / raw)
  To: guile-user

On 04/25/2017 11:50 AM, Jan Wedekind wrote:

> What do people think?  I'm struggling with deciding what's the right
> thing for my own code, but leaning towards "we shouldn't use the ?
> suffix for just boolean values".

I like using the '?' suffix only for predicates.  I often use 'is-' 
prefix for boolean values, which reads well: (if is-raining ...)

-Paul



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
  2017-04-25  3:43 ` Alex Vong
@ 2017-04-25 16:53 ` Panicz Maciej Godek
  2017-04-26  0:23 ` Thompson, David
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Panicz Maciej Godek @ 2017-04-25 16:53 UTC (permalink / raw)
  To: Christopher Allan Webber; +Cc: guile-user@gnu.org

Hi :)

2017-04-24 19:01 GMT+02:00 Christopher Allan Webber <cwebber@dustycloud.org>
:

> Hello everyone!  Here's a little bikeshed for us to paint.
>
> I've noticed that it's common in Guile modules to use "foo?" for
> variable names involving booleans.  It's tempting, because this looks
> an awful lot like you're asking a question... and it's also common
> for this even to be keyword arguments to procedures, etc.
>
> But is it a good idea?  I thought "foo?" was supposed to be for
> predicates, as a nicer version of the "foo-p" predicate convention in
> other non-scheme lisps.  I can't imagine other lisps doing "foo-p" for
> just variables with boolean values.
>
> On the other hand, once you start adding ? to the end of boolean'y
> things, it *does* become tempting to put them at the end of boolean
> variables and arguments.  It looks pretty nice.
>
> What do people think?  I'm struggling with deciding what's the right
> thing for my own code, but leaning towards "we shouldn't use the ?
> suffix for just boolean values".
>

I think that there's no need to be super-consistent here.
Actually, I can't think of any natural and elegant use of a boolean variable
from the top of my head, but my gutt feeling is that it usually can be
avoided
(however, if you have some particular examples on your mind, I think
it could be instructive to see them here).

Also, there seems to be many aspects of this. When we're using
a name inside a let or let* form, it shouldn't be very harmful to use
question mark
in the name of an intermediate variable. The thing with function parameters
and global names are I believe more important, because they are a part of
some interface.

As for parameter names, I'm not sure whether it is a good idea to use
question marks in the names that ought to refer to predicates. I'm not sure
if it is better for a prototype of "filter" to be

(filter condition? list)

or

(filter condition list)

Actually, I find it more natural to interpret the predicate as a boolean
flag
to a function, rather than a predicate. That said, I think it is a bad
practice
to make boolean flags arguments to functions, because it is impossible
to say how (f #false #true #false) differs from (f  #true #true #true) nor
what the roles of these arguments are (which is why I think the interface
of "format" sucks)

Hinldley-Milner languages avoid this problem, because a type signature
tells more than just a name (and is more reliable), and they handle tagged
unions easily, and it is much better to define a union and use one of its
values, than a boolean value (and using Lisp symbols for this purpose,
although more readable, is error prone and likely less efficient)

Best regards,
Panicz


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-25  3:43 ` Alex Vong
  2017-04-25 15:50   ` Jan Wedekind
@ 2017-04-25 18:08   ` Neil Jerram
  1 sibling, 0 replies; 13+ messages in thread
From: Neil Jerram @ 2017-04-25 18:08 UTC (permalink / raw)
  To: guile-user

I'm also not sure.  In the past I've written code with an 'xxx?' 
variable, and then added a procedure to set it, giving 'set-xxx?!' - 
which is not exactly elegant.

Also I'm not sure it's best practice to have many 
variables/attributes/parameters with boolean values, as this can lead to 
code that has too many opaque uses of #t and #f.  (I.e. it's often 
unclear, at the point of use, what each #t or #f means.)  It might be 
better to refactor to something with non-boolean values, e.g. from

    (set! red? #t)
    (set! narrow?#t)

to

    (set! properties '(red narrow))

And in that case the question about '?' would disappear.

Regards,
       Neil


On 25/04/17 04:43, Alex Vong wrote:
> Good question! I can't decide as well. I want to know how people think
> about it.
>
> In a lazy language, a variable is a 0-ary thunk, while a predicate is a
> 1-ary thunk. Since they are really just special case of a general thing,
> it make sense to use foo? for both cases. But we all know guile is not
> lazy, so I really don't know what to do.
>
> Christopher Allan Webber <cwebber@dustycloud.org> writes:
>
>> Hello everyone!  Here's a little bikeshed for us to paint.
>>
>> I've noticed that it's common in Guile modules to use "foo?" for
>> variable names involving booleans.  It's tempting, because this looks
>> an awful lot like you're asking a question... and it's also common
>> for this even to be keyword arguments to procedures, etc.
>>
>> But is it a good idea?  I thought "foo?" was supposed to be for
>> predicates, as a nicer version of the "foo-p" predicate convention in
>> other non-scheme lisps.  I can't imagine other lisps doing "foo-p" for
>> just variables with boolean values.
>>
>> On the other hand, once you start adding ? to the end of boolean'y
>> things, it *does* become tempting to put them at the end of boolean
>> variables and arguments.  It looks pretty nice.
>>
>> What do people think?  I'm struggling with deciding what's the right
>> thing for my own code, but leaning towards "we shouldn't use the ?
>> suffix for just boolean values".
>>
>>   - Chris




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
  2017-04-25  3:43 ` Alex Vong
  2017-04-25 16:53 ` Panicz Maciej Godek
@ 2017-04-26  0:23 ` Thompson, David
  2017-04-27 10:39 ` Jan Nieuwenhuizen
  2017-05-05 17:26 ` Vladimir Zhbanov
  4 siblings, 0 replies; 13+ messages in thread
From: Thompson, David @ 2017-04-26  0:23 UTC (permalink / raw)
  To: Christopher Allan Webber; +Cc: guile-user@gnu.org

Hey,

On Mon, Apr 24, 2017 at 1:01 PM, Christopher Allan Webber
<cwebber@dustycloud.org> wrote:
>
> What do people think?  I'm struggling with deciding what's the right
> thing for my own code, but leaning towards "we shouldn't use the ?
> suffix for just boolean values".

My convention is that anything that evaluates to either #t or #f has a
'?' at the end.  Thus both predicates and flag variables are named
like this.  Using 'is-' or something like that is just how languages
that don't allow punctuation in symbols workaround the limitation.
Ruby is particularly annoying because while a method name can have a
question mark at the end, a variable name cannot.

tl;dr - Good idea!

- Dave



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-25 16:21     ` Paul
@ 2017-04-26 15:25       ` Alex Kost
  0 siblings, 0 replies; 13+ messages in thread
From: Alex Kost @ 2017-04-26 15:25 UTC (permalink / raw)
  To: Paul; +Cc: guile-user

Paul (2017-04-25 12:21 -0400) wrote:

> On 04/25/2017 11:50 AM, Jan Wedekind wrote:
>
>> What do people think?  I'm struggling with deciding what's the right
>> thing for my own code, but leaning towards "we shouldn't use the ?
>> suffix for just boolean values".
>
> I like using the '?' suffix only for predicates.  I often use 'is-' 
> prefix for boolean values, which reads well: (if is-raining ...)

Thanks for sharing!  I also like to use '?' for functions but I always
felt uncomfortable to use it for variables.  Using "is-" (or "has-" or
alike) sounds interesting, I'm going to try this convention in my code,
thanks for the idea!

-- 
Alex



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
                   ` (2 preceding siblings ...)
  2017-04-26  0:23 ` Thompson, David
@ 2017-04-27 10:39 ` Jan Nieuwenhuizen
  2017-04-27 10:53   ` Panicz Maciej Godek
  2017-04-27 12:07   ` Marko Rauhamaa
  2017-05-05 17:26 ` Vladimir Zhbanov
  4 siblings, 2 replies; 13+ messages in thread
From: Jan Nieuwenhuizen @ 2017-04-27 10:39 UTC (permalink / raw)
  To: Christopher Allan Webber; +Cc: guile-user@gnu.org

Christopher Allan Webber writes:

> I've noticed that it's common in Guile modules to use "foo?" for
> variable names involving booleans.  It's tempting,

> But is it a good idea?

It's an idea that I like and use.  Not sure that says anything about
good or bad.

I would like to help you paint though!

We have functions like null? and pair? that return booleans, where I
would like the [non]-nil value.  I often find myself writing things like

   (let ((bar (if (pair? foo) (baz foo)
               #f)))
   
where I would rather like to write something like

   (let ((bar (and=> (pair?=> foo) baz)))

How do you do these things, and how do you call your pair?=> function?

Greetings, --janneke

-- 
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl  



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-27 10:39 ` Jan Nieuwenhuizen
@ 2017-04-27 10:53   ` Panicz Maciej Godek
  2017-04-27 12:07   ` Marko Rauhamaa
  1 sibling, 0 replies; 13+ messages in thread
From: Panicz Maciej Godek @ 2017-04-27 10:53 UTC (permalink / raw)
  To: Jan Nieuwenhuizen; +Cc: guile-user@gnu.org

2017-04-27 12:39 GMT+02:00 Jan Nieuwenhuizen <janneke@gnu.org>:

> Christopher Allan Webber writes:
>
> > I've noticed that it's common in Guile modules to use "foo?" for
> > variable names involving booleans.  It's tempting,
>
> > But is it a good idea?
>
> It's an idea that I like and use.  Not sure that says anything about
> good or bad.
>
> I would like to help you paint though!
>
> We have functions like null? and pair? that return booleans, where I
> would like the [non]-nil value.  I often find myself writing things like
>
>    (let ((bar (if (pair? foo) (baz foo)
>                #f)))
>
> where I would rather like to write something like
>
>    (let ((bar (and=> (pair?=> foo) baz)))
>
> How do you do these things, and how do you call your pair?=> function?
>
>
I usually use the and-let* form (SRFI-2) to deal with  such cases.

(and-let* (((pair? foo)))
  (baz foo))

The (grand scheme) library which I maintain provides a variant of and-let*
that additionally provides pattern matching:

https://github.com/plande/grand-scheme/blob/master/grand/syntax.scm#L255

so you could even write the above code as

(and-let* (((_ . _) foo))
  (baz foo))


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-27 10:39 ` Jan Nieuwenhuizen
  2017-04-27 10:53   ` Panicz Maciej Godek
@ 2017-04-27 12:07   ` Marko Rauhamaa
  1 sibling, 0 replies; 13+ messages in thread
From: Marko Rauhamaa @ 2017-04-27 12:07 UTC (permalink / raw)
  To: Jan Nieuwenhuizen; +Cc: guile-user@gnu.org

Jan Nieuwenhuizen <janneke@gnu.org>:

> Christopher Allan Webber writes:
>
>> I've noticed that it's common in Guile modules to use "foo?" for
>> variable names involving booleans.  It's tempting,
>
>> But is it a good idea?
>
> It's an idea that I like and use.

There are also the traditional:

   (let ((name$ "Henry Ford")
         (birth-year% 1863)
         (timestamp& 1493294649)
         (price@ 99.99)
         (slice! 0.6666667)
         (proportion# 0.66666666666666666666667))
      ...)


Marko



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
                   ` (3 preceding siblings ...)
  2017-04-27 10:39 ` Jan Nieuwenhuizen
@ 2017-05-05 17:26 ` Vladimir Zhbanov
  2017-05-05 19:44   ` szgyg
  4 siblings, 1 reply; 13+ messages in thread
From: Vladimir Zhbanov @ 2017-05-05 17:26 UTC (permalink / raw)
  To: guile-user

? could be a prefix for booleans in contrast with predicates:

(define ?red-x (red? x))
...
(if (red? x) 'a 'b)
...
(if ?red-x 'a 'b)

-- 
  Vladimir



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: ?-suffix for booleans... good-idea? or bad-idea?
  2017-05-05 17:26 ` Vladimir Zhbanov
@ 2017-05-05 19:44   ` szgyg
  0 siblings, 0 replies; 13+ messages in thread
From: szgyg @ 2017-05-05 19:44 UTC (permalink / raw)
  To: guile-user

On Fri, May 05, 2017 at 08:26:03PM +0300, Vladimir Zhbanov wrote:
> ? could be a prefix for booleans in contrast with predicates:
> 
> (define ?red-x (red? x))
> ...
> (if (red? x) 'a 'b)
> ...
> (if ?red-x 'a 'b)

?foo is used for macro template variables.

(define-syntax begin1
  (syntax-rules ()
    ((_ ?first ?body ...)
     (let ((tmp ?first)) ?body ... tmp))))

With this convention it's clear that ?first and ?body will be replaced,
while let and tmp will appear literally in the expanded macro.

For macro-generating macros we can also use ??foo

(define-syntax-rule (create-useless-macro ?name)
  (define-syntax-rule (?name ??param)
    (display '(?name ??param))))

(create-useless-macro foo)
=>
(define-syntax-rule (foo ??param)
  (display '(foo ??param)))

(foo bar)
=>
(display '(foo bar))


This convention is used in SRFI-46[0] for example.

szgyg

[0] https://srfi.schemers.org/srfi-46/srfi-46.html



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2017-05-05 19:44 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-24 17:01 ?-suffix for booleans... good-idea? or bad-idea? Christopher Allan Webber
2017-04-25  3:43 ` Alex Vong
2017-04-25 15:50   ` Jan Wedekind
2017-04-25 16:21     ` Paul
2017-04-26 15:25       ` Alex Kost
2017-04-25 18:08   ` Neil Jerram
2017-04-25 16:53 ` Panicz Maciej Godek
2017-04-26  0:23 ` Thompson, David
2017-04-27 10:39 ` Jan Nieuwenhuizen
2017-04-27 10:53   ` Panicz Maciej Godek
2017-04-27 12:07   ` Marko Rauhamaa
2017-05-05 17:26 ` Vladimir Zhbanov
2017-05-05 19:44   ` szgyg

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).