unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Taylan Kammer <taylan.kammer@gmail.com>
To: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>,
	Guile User <guile-user@gnu.org>
Subject: Re: Surprising behavior of eq?
Date: Sun, 20 Sep 2020 19:26:35 +0200	[thread overview]
Message-ID: <7dccb9ee-7da2-d1b0-2913-7a70bb547e05@gmail.com> (raw)
In-Reply-To: <c662512f-c5fb-b7c5-071b-4fe11bc87229@posteo.de>

Others already mentioned 'equal?' as the correct predicate for testing 
structural content equality.  It works not only for strings but also 
arbitrarily nested structures, for instance:


   (define a (list 'a 9871239876234 "foo" (vector #\a #\b #\c)))

   (define b '(a 9871239876234 (string #\f #\o #\o) #(#\a #\b #\c)))

   (equal? a b) ;=> #t


   (define c "foo")

   (define d 1234)

   (equal? c d) ;=> #f  (obviously)


But if you know that two variables both reference a string object, 
there's a more appropriate predicate which explicitly tests for string 
equality: 'string=?'


   (define a "foo")

   (define b (string #\f #\o #\o))

   (string=? a b) ;=> #t


   (define c "foo")

   (define d 1234)

   (string=? c d) ;=> ERROR: wrong type argument 1234 passed to string=?


Using 'string=?' instead of 'equal?' has a few small advantages in this 
case:

- It makes it obvious to the reader of the code that two variables are 
both supposed to reference a string.

- If due to some strange mistake one or both of the variables end up 
referencing an object that is not a string, the 'string=?' call will 
immediately raise an exception, which is probably better than it just 
returning #f and letting the code continue to run, because the code will 
probably crash at a later point anyway due to the object with the wrong 
type.  (It's best for code to crash as soon as possible when an error 
like that happens, so you can find the root cause easily.)

- Lastly, 'string=?' probably has a tiny performance advantage, though 
that's probably not important here.  (If you experience performance 
issues with your code, you should benchmark it to see where the actual 
issue is.  I'm just mentioning this for completeness' sake.)


Of course if your variables may contain different types of values like 
lists and vectors and not just strings, you should indeed use 'equal?'.


- Taylan



      parent reply	other threads:[~2020-09-20 17:26 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-20 12:16 Surprising behavior of eq? Zelphir Kaltstahl
2020-09-20 12:19 ` Zelphir Kaltstahl
2020-09-20 12:58   ` Christopher Lemmer Webber
2020-09-20 13:09   ` Zelphir Kaltstahl
2020-09-20 13:52     ` John Cowan
2020-09-20 15:37       ` Zelphir Kaltstahl
2020-09-20 16:18         ` tomas
2020-09-20 17:05         ` John Cowan
2020-09-20 20:51           ` Linus Björnstam
2020-09-20 21:42             ` John Cowan
2020-09-20 13:57     ` Stefan Schmiedl
2020-09-20 15:42       ` Zelphir Kaltstahl
2020-09-20 17:26 ` Taylan Kammer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7dccb9ee-7da2-d1b0-2913-7a70bb547e05@gmail.com \
    --to=taylan.kammer@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).