Lisp: advanced string comparison
Asked Answered
L

1

10

I recently encountered this line in some common LISP library code:

(string-equal #1="http://" url :end2 (min (length url) #2=#.(length #1#)))

Here, url was passed in as a string variable. I understand the purpose of this comparison is to determine if the url string starts with http:// and it's a case-insensitive compare. I also understand about string-equal keys, such as :start and :end. But the pound sign (#) items threw me. I can figure most of it out by context, but I haven't found documentation on how it works, and I'm still befuddled a bit by what #2=#.(length #1#) really means. It looks a little mystical to me.

Could someone please explain how the pound sign mechanism works in this specific context and if it's universally usable in other constructs in the same way? Or point me to a document/website that describes it.

Thank you!

Loppy answered 2/1, 2014 at 4:4 Comment(0)
B
12

The pound (or sharp) sign's function is described in the Hyperspec here.

The #1= notation labels the following form (here the string "http://") with a numeric index for later backreference by the #1# notation. #. causes the following form to be evaluated at read time.

The overall effect is to make it as if the code had been written as:

(string-equal "http://" url :end2 (min (length url) 7))
Bowden answered 2/1, 2014 at 4:23 Comment(2)
Thanks Sean. Why would the programmer have the #2= as well? I'm supposing that it's superfluous in this case.Loppy
It appears superfluous to me as well.Bowden

© 2022 - 2024 — McMap. All rights reserved.