[racket] Racketrivia: Using ' as an identifier suffix

28 views
Skip to first unread message

Greg Hendershott

unread,
Jan 8, 2015, 3:26:06 PM1/8/15
to us...@racket-lang.org
So Haskell conventionally uses ' as a suffix, prime. From what I've
seen, Scheme and Racket tend to use * instead.

At some point I "learned" that you cannot use ' as a suffix in Racket.

Today I tried again, and was surprised to see that it works... somewhat.

$ racket
Welcome to Racket v6.1.1.6.
-> (define x' 42)
-> x'
42
-> (+ x' 10)
'(+ x '10)
-> (+ 10 x')
; readline-input:4:8: read: unexpected `)'
-> (+ 10 x' )
; readline-input:5:8: read: unexpected `)'


0. It turns out x' _is_ a valid identifier, and it self-evaluates just
fine. Interesting.

1. I don't understand why (+ x' 10) evaluates not to 52, and not even
an error, but... '(+ x '10). WAT.

2. Less surprising to me is that (+ 10 x') and even (+ 10 x' ) are
errors. But actually, I wonder why the reader (or lexer?) couldn't
handle ' followed by a character that can't be part of an identifier?


p.s. I'm not proposing this would be a great suffix style to use.
Quick, distinguish x' from 'x ! And don't type one when you mean the
other! I get that. Even so, I'm curious.
____________________
Racket Users list:
http://lists.racket-lang.org/users

Robby Findler

unread,
Jan 8, 2015, 3:34:32 PM1/8/15
to Greg Hendershott, Racket Users
You are being so terribly tricked by the reader here.

x followed by ' is never an identifer.

(define x' 42)

is the same as

(define x '42)

which is the same as

(define x 42)

But then the terribleness comes in.

x'

reads a "x" followed by an unfinished expression. So when you do:

> x'
42

and then the next thing you type is going to be quoted.

Welcome to Racket v6.1.1.6.
> (define x 1)
> x'
1
> quoteme
'quoteme


Robby

Matthias Felleisen

unread,
Jan 8, 2015, 3:42:47 PM1/8/15
to Greg Hendershott, us...@racket-lang.org

On Jan 8, 2015, at 3:15 PM, Greg Hendershott <greghen...@gmail.com> wrote:

> So Haskell conventionally uses ' as a suffix, prime. From what I've
> seen, Scheme and Racket tend to use * instead.
>
> At some point I "learned" that you cannot use ' as a suffix in Racket.
>
> Today I tried again, and was surprised to see that it works... somewhat.
>
> $ racket
> Welcome to Racket v6.1.1.6.
> -> (define x' 42)

This definition introduces x as '42. Works like a charm


> -> x'

This reference derefs x, which is 42. And it introduces a ' into the io stream.


> 42
> -> (+ x' 10)

Here the quote is applied to an S-expression and you get exactly the expected result.


> '(+ x '10)
> -> (+ 10 x')

This is + 10 x [as defined above] and the ' token. There is one place where it can't appear: to the left of a )


> ; readline-input:4:8: read: unexpected `)'
> -> (+ 10 x' )

Same here.


;; ---

In principle, ' would be good to use as part of a name but I prefer to use it for quoted S-expressions if that's the choice I have. Then again, I'd be willing to give up ' in favor of ` if this didn't have performance problems.

-- Matthias

Jon Zeppieri

unread,
Jan 8, 2015, 3:50:30 PM1/8/15
to Greg Hendershott, Racket Users
You can use the unicode prime character (′) as a suffix. In DrRacket,
you can type that as \prime followed by ctrl-\. It would be a lot
nicer to map it to a simple key combo like λ is,

-Jon

Robby Findler

unread,
Jan 8, 2015, 3:58:22 PM1/8/15
to Jon Zeppieri, Racket Users
That's a scary convention. It's hard for humans to see the difference!

Deren Dohoda

unread,
Jan 8, 2015, 4:06:30 PM1/8/15
to Greg Hendershott, Racket mailing list
Welcome to DrRacket, version 6.0.1 [3m].
Language: racket/base [custom]; memory limit: 4096 MB.
> (string->symbol "x'")
'|x'|



On Thu, Jan 8, 2015 at 3:15 PM, Greg Hendershott <greghen...@gmail.com> wrote:

Jon Zeppieri

unread,
Jan 8, 2015, 4:14:00 PM1/8/15
to Robby Findler, Racket Users
When it's at the end of an identifier, it's pretty clear. There's no
good reason to put a single-quote there. -Jon

J. Ian Johnson

unread,
Jan 8, 2015, 4:22:18 PM1/8/15
to robby, users
Indeed, I tried it for a while and didn't like it. I use * instead of ' for primes in my code. I think that's pretty common practice.
-Ian

Jon Zeppieri

unread,
Jan 8, 2015, 4:30:29 PM1/8/15
to J. Ian Johnson, users, robby
I'm under the impression that * usually indicates some kind of
pluralization or Kleene star-esque behavior.

J. Ian Johnson

unread,
Jan 8, 2015, 4:38:08 PM1/8/15
to Jon Zeppieri, users, robby
The * convention for list* and hash-set* is an ad-hoc, wiggly, underspecified "convention." I would claim the naming of those functions as an historical accident, and shouldn't limit one from using * as primes.

Greg Hendershott

unread,
Jan 8, 2015, 4:45:57 PM1/8/15
to Robby Findler, Racket Users
> You are being so terribly tricked by the reader here.

Thanks for the explanation! It all makes sense now.

I'm not sure whether to be chagrined or perversely proud that I
managed to stumble onto this sequence. :)

Robby Findler

unread,
Jan 8, 2015, 4:54:15 PM1/8/15
to Greg Hendershott, Racket Users
Clearly proud. That's a helluva sequence. I had to scratch my head
multiple times before replying.

Robby

Greg Hendershott

unread,
Jan 8, 2015, 5:03:15 PM1/8/15
to Robby Findler, Racket Users
It's not as fun as the classic https://www.destroyallsoftware.com/talks/wat.

But this is Racket not JavaScript. ;)

Neil Van Dyke

unread,
Jan 8, 2015, 5:10:04 PM1/8/15
to users
I generally recommend against using non-ASCII characters in Racket
source code.

Besides the human readability question (and the searchability question),
non-ASCII still presents a significant risk of your characters getting
corrupted "in transmission". Especially with all the Web sites and
other network services through which code might be presented and
copy&pasted. It's still not uncommon for a site/service to send
non-ASCII characters under the wrong encoding labeling, and you'll even
see services that map problematic Unicode characters (e.g., special
spaces, dashes, quotes) to ASCIII.

One forgivable exception to ASCII-only is if you want to be cute in code
examples in an academic paper. Although you can still run into
transmission problems, when people copy&paste code, and when your PDFs
are translated to a different format.

Then there is the cute lambda character, which has become a special
case, since it's been adopted by some Racket developers. I think some
of the appeal of that might go away if there were a more terse lambda
syntax. Maybe just make `fn` an alias for `lambda`. (nd, as an old
programmer friend (who was generally impressed when I explained Scheme's
first-class closures to him) remarked, "They actually type out the word
'lambda'? That's *so precious*." I've actually grown to find typing
out `lambda` to be tolerable, but if people are going to use a more
terse alias, `fn` is not problematic like the lambda character.

Neil V.

Alexander D. Knauth

unread,
Jan 8, 2015, 5:19:11 PM1/8/15
to Robby Findler, racket users list
Why isn’t x’ either a reader error or an unfinished expression (like an unclosed paren where it allows a multi-line expression)?

I notice DrRacket treats it the 2nd way (which makes perfect sense to me) but the command-line repl and (read-eval-print-loop) both do the weird thing where it evaluates the x and saves the quote for the next expression.

Is there a reason that they don’t all do what DrRacket does?

Pierpaolo Bernardi

unread,
Jan 8, 2015, 5:31:06 PM1/8/15
to Neil Van Dyke, users
On Thu, Jan 8, 2015 at 10:49 PM, Neil Van Dyke <ne...@neilvandyke.org> wrote:
> I generally recommend against using non-ASCII characters in Racket source
> code.
>
> Besides the human readability question (and the searchability question),
> non-ASCII still presents a significant risk of your characters getting
> corrupted "in transmission".

Unicode is here to stay, you can't sweep it under the rug. The more
we use it, the sooner people will get annoyed with broken services and
protocols and the sooner the brokenness will go away.

And in addition to cute symbols, there are also normal character for
writing languages other than English.

Neil Van Dyke

unread,
Jan 8, 2015, 5:39:02 PM1/8/15
to Pierpaolo Bernardi, users
Pierpaolo Bernardi wrote on 01/08/2015 05:20 PM:
> Unicode is here to stay, you can't sweep it under the rug. The more
> we use it, the sooner people will get annoyed with broken services and
> protocols and the sooner the brokenness will go away.
>
> And in addition to cute symbols, there are also normal character for
> writing languages other than English.

ಠ_ಠ

http://www.disapprovallook.com/

Jon Zeppieri

unread,
Jan 8, 2015, 5:46:35 PM1/8/15
to users
On Thu, Jan 8, 2015 at 4:49 PM, Neil Van Dyke <ne...@neilvandyke.org> wrote:
> I generally recommend against using non-ASCII characters in Racket source
> code.
>

I used to do the same -- not so much for the readability issue, since
often using non-ASCII characters aids readability, but for the
character encoding problems that were bound to cause trouble. They
just don't any more, however, not with the tools and services I use. I
don't doubt that there are still sites that have these problems, but
they're no longer the norm, and I simply don't worry about it these
days.

Jay McCarthy

unread,
Jan 8, 2015, 6:02:29 PM1/8/15
to Jon Zeppieri, users
I love Unicode:

http://tinyurl.com/n7phqyv
--
Jay McCarthy
http://jeapostrophe.github.io

"Wherefore, be not weary in well-doing,
for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
- D&C 64:33

Norman Gray

unread,
Jan 8, 2015, 6:53:42 PM1/8/15
to Greg Hendershott, Racket Users

> On 2015 Jan 8, at 21:36, Greg Hendershott <greghen...@gmail.com> wrote:
>
> It's not as fun as the classic https://www.destroyallsoftware.com/talks/wat.

...or indeed as the perennial delight that is xii.tex <http://www.ctan.org/tex-archive/macros/plain/contrib/xii>

Norman


--
Norman Gray : http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK

Neil Van Dyke

unread,
Jan 8, 2015, 7:02:43 PM1/8/15
to Pierpaolo Bernardi, users
Pierpaolo has good points, and so I wouldn't put my recommendation as
strongly as before.

However, I'll still keep non-ASCII out of any code I write, for the
reasons I stated.

Incidentally, a lot of Racket code is being run in/for production
environments that are still using a lot of very-old-version Web
browsers, and sometimes even with old not-entirely-transparent HTTP
proxies. The requirements (express and implied) are sometimes such that
one must conform to the existing environment -- you can't always say,
"You are not in compliance with standard 12345, so the failure was your
responsibility," nor "Well, this wouldn't have failed, had you gotten
with the times, so let this be a learning experience for you, you're
welcome." This is also related to the protocol guidance of "be liberal
in what you accept, and conservative in what you send."

Neil V.

Pierpaolo Bernardi wrote on 01/08/2015 05:20 PM:
Reply all
Reply to author
Forward
0 new messages