in reply to Question Marks in Subroutine Names

Okay...

To begin with, I'm related to this kid, so take what I say with a grain of salt.

But.

TMTOWTDI.

End of story. Anybody who's used LISP knows that this is a great feature. Subroutines that evaluate to true or false should be distinguished from ones that return a value.

is_female() should return an object of type female or FALSE. is_female?() should simply return TRUE or FALSE. It's faster, you can short-circuit up the ying-yang and it's just generally better.

Some implementations of LISP use conventions like is_femalep (stands for predicate) and that's pretty nice too. All in all, you should be able to do this sort of thing in Perl, because TMTOWTDI.

My two cents,
David.

Replies are listed 'Best First'.
Re: Re: Question Marks in Subroutine Names
by ariels (Curate) on May 08, 2002 at 07:56 UTC

    Anyone who's used Lisp knows that only Scheme heathen put question marks in predicates. Lisp (and Common Lisp) use the -P convention to mark predicates.

    You could to the same in Perl (use a -<samp>p</samp> or -<samp>_p</samp> suffix on predicates). is_female would become femalep, and Lisp hackers would feel right at home. Perl hackers might be somewhat less pleased, though.

    The reason you can do this sort of thing (use question marks in names) in Scheme (and could do it in Common Lisp, even though it isn't done) is that Lisps have a very simple lexical texture. In contrast, Perl has a horribly complex lexical texture to it, using every symbol on my keyboard (and overloading most of them). But any language using the C "ternary operator" ?: (and not promoting whitespace as a required separator) will not be able to have question marks in symbols.

Re: Re: Question Marks in Subroutine Names
by ChemBoy (Priest) on May 08, 2002 at 05:52 UTC

    I'm always amused by something that says "end of story" at the beginning of the third paragraph from the end... :-) I'm also curious about some of your sweeping statements, here, because they don't make instant sense to me, and you didn't give me much to work with in figuring them out.

    To begin with, I haven't ever "used" Lisp, though I'm somewhat familiar with it, but from Lisp's general reputation, I'm assuming that when you say "faster" you don't mean "faster in execution time", since if you were worrying about execution time you'd be using something... er, faster. :-) So do please correct me if I'm wrong on that (since I'm going to ignore that consideration henceforth).

    That said, where is the advantage to the division of function names you propose? I can imagine some cases where it would be advantageous to have your test function return an object rather than a simple "to thine own self be true", certainly (though I would guess fewer in Perl than Lisp), but in that case, why have a second function which does the same amount of work and returns something else? Since the you can return any old Perl object and have it test True in boolean context, what's the need to have two functions for it?

    "Subroutines that evaluate to true or false should be distinguished from ones that return a value."

    Generally speaking, I agree--but I don't think functions that ask true-false questions should return much other than true or false, personally. Assuming that you do (for reasons hypothecated above) want to do so, why not do it all the time?

    None of this, of course, goes to show that your points are wrong, but merely that I do not understand quite what you're getting at--might I be enlightened?



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

      I'm assuming that when you say "faster" you don't mean "faster in execution time", since if you were worrying about execution time you'd be using something... er, faster. So do please correct me if I'm wrong on that

      You're wrong on that. :-) Good modern Lisp implementations can move along pretty darn well.

Re: Re: Question Marks in Subroutine Names
by demerphq (Chancellor) on May 08, 2002 at 19:18 UTC
    Im a little confused. While I'm well aware of the convention of using the _p -p p suffix for predicate I really dont see how it or a question mark makes the functions following the is_XXXX covention (ie is_female()) more readable.

    Its very clear (at least to me) that an is_XXXXX function returns a boolean (well, perls closest equivelent). What benefit do you derive from the extra marker? I can see how a function _not_ named is_XXXXX might need it, but then again we have a convention for that: is_XXXXXX.

    (partly serious, partly not)

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      1) I believe the use of p was intended as a replacement for the other forms
      2) I see it as an analog of "Hungarian notation"

      --
      perl -pew "s/\b;([mnst])/'$1/g"

        2) I see it as an analog of "Hungarian notation"

        You mean woefully painful and innappropriate to a language like perl?

        ;-)

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.

Re: Re: Question Marks in Subroutine Names
by hding (Chaplain) on May 08, 2002 at 12:56 UTC

    To be more precise, in Scheme it seems to be traditional to end this type of function with a '?'. In Common Lisp, depending on whether or not the rest of the function's name has a '-' in it or not, one ends it with -p or p (e.g one would have simple-string-p and stringp). (I think there may be an exception to this, but it escapes me at the moment. And I don't think this is followed strictly for certain functions which get automatically defined when using defstruct - which may even be the exceptions I'm thinking of.) I do not know about other Lisp dialects.