From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Taylan Kammer Newsgroups: gmane.lisp.guile.user Subject: Re: Surprising behavior of eq? Date: Sun, 20 Sep 2020 19:26:35 +0200 Message-ID: <7dccb9ee-7da2-d1b0-2913-7a70bb547e05@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7194"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 To: Zelphir Kaltstahl , Guile User Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 20 19:26:55 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kK371-0001hR-53 for guile-user@m.gmane-mx.org; Sun, 20 Sep 2020 19:26:55 +0200 Original-Received: from localhost ([::1]:35226 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kK370-0003yo-6k for guile-user@m.gmane-mx.org; Sun, 20 Sep 2020 13:26:54 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kK36m-0003xF-Vm for guile-user@gnu.org; Sun, 20 Sep 2020 13:26:40 -0400 Original-Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:41232) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kK36l-0007EI-5r for guile-user@gnu.org; Sun, 20 Sep 2020 13:26:40 -0400 Original-Received: by mail-wr1-x42d.google.com with SMTP id w5so10386193wrp.8 for ; Sun, 20 Sep 2020 10:26:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=ffkGWZy6P+xSYOhpx9RluROtr/7j3+8/KYmCC2oew1E=; b=McgoluW5LWTX6TRAVtaVaOczOMhP0/138g8oJrMNrpY6qA4bBuAhyanbaGVbyYRx2y mGjGk4+KrBdwHU/OkLFKIU/BjXhntqFUlXAgLluZAoMp1i1I8qLc4GsjC9fkfhu6ADWZ MyDLE84E4VirgWddkgKmjt1d3jnqQCP5J/xSuz7TD1bxxDDQv7qSykIXvLwYWpdegOXZ BKSqT/WdsJke4ysYKXB8TdFw0sjMwGkFfN/0wOk1tvs3HpGZfrssescnr6wmVNKekPqy 9kSOySoHU/cgfYVplfd6s7YnFLJEy20mfI0e1p/0iYbK5VxRLSFUo39sIsZHgrmbZqHp l4ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ffkGWZy6P+xSYOhpx9RluROtr/7j3+8/KYmCC2oew1E=; b=rOmeXp0pw3lAfiWmxPX86B77RvCySv4Ppf9N9IXoyjzlVos1dxLzRpa0uRn0nf/Lqi tiu60lwmAHpGXuoRdCq3Q4aPe7nzjxl4NzIqEkaGv2jBSCnCgajym3d3VmSLNqKeKt2I BdJNKtmbwhqp07VL43HyxAP7yLKoelbk8KjbWvsL5IL5CyuyMvEHwRU09C4RJfjjeD0X HUmSFI/uxT140DxHXO9LYGUv66XOtkZdfSYD+0h3mvoA0BLohTbrDPmo1U7dXzOu0s4C k7OPC2FgKsWkedXhqdWhgrVRppPkAuaEC/bBm85bPhtRzMsRI1VxvtRKNp+MTXMwJoc+ JjZw== X-Gm-Message-State: AOAM532VoqMyXoMs+lV5psoIkf7o8DBZyitv9QPqOjGev+QzKv/qVM1Q m/WNOMeE3Er7+ugGujEckgKgB2fToa8= X-Google-Smtp-Source: ABdhPJzRt2VQZPdJ82j/wW/2ZDmoj8/eymxW+2SNdYpMOIHqe8mJ7GPiKbhvUe2Rklmmbvk1dGUINg== X-Received: by 2002:a05:6000:1c8:: with SMTP id t8mr47937882wrx.3.1600622796749; Sun, 20 Sep 2020 10:26:36 -0700 (PDT) Original-Received: from ?IPv6:2a02:908:c71:ba60:c9c6:b0f0:9217:de76? ([2a02:908:c71:ba60:c9c6:b0f0:9217:de76]) by smtp.gmail.com with ESMTPSA id u8sm14867002wmj.45.2020.09.20.10.26.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 20 Sep 2020 10:26:36 -0700 (PDT) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=taylan.kammer@gmail.com; helo=mail-wr1-x42d.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16940 Archived-At: 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