Subject: Re: Primitives for "stings"
From: Erik Naggum <erik@naggum.net>
Date: Mon, 22 Oct 2001 02:09:12 GMT
Newsgroups: comp.lang.lisp
Message-ID: <3212705349358958@naggum.net>

* Kent M Pitman
| It's one of those newbie "intuitions" that we who understand the issues
| are all-too-quick to stomp on before thining "gee, maybe this isn't such
| an awful idea".  But then again, it ISN'T how you do things in CL, so
| newbies do need to straightened out about the realities at some point.

(defun qar (string)
  "Return the first element of string."
  (char string 0))

(defun qdr (string)
  "Return the rest of string, or nil if exhausted."
  (multiple-value-bind (displaced-to index-offset)
      (array-displacement string)
    (let ((length (1- (length string))))
      (if displaced-to
	  (if (plusp length)
	      (adjust-array string length
			    :displaced-to displaced-to
			    :displaced-index-offset (1+ index-offset))
	    nil)
	(make-array length
		    :element-type (array-element-type string)
		    :adjustable t
		    :displaced-to string
		    :displaced-index-offset 1)))))

  I believe this is how they do "pointers" in C++ STL Strings, too¹, so it
  should not frighten those who would have abhorred an insane overhead had
  they looked under the hood.  And years from now, somebody will look at
  this code and not understand why it was a joke in 2001, when Guarana or
  whatever the next C-(for-caffeine)-related programming language will be
  called, needs a new way to waste _all_ the computrons the hardware folks
  have given them.

  And the _real_ irony here is that using a string as an array with an
  offset is no slower than stepping a pointer on most new architectures.
  Even back in the PDP-11 days, they had addressing modes that allowed
  array indexing at _very_ low cost.  Of course, given that every single
  machine cycle back then cost human sweat and pain, the fathers of C found
  a way to optimize the language accordingly, and non-quiche-eating, "real"
  C programmers abhor "the Pascal way" of using strings as arrays to this
  day, when it actually hurts the prediction logic in modern processors
  because the hardware people cannot fathom why software people still hand-
  optimize their low-level C code for a PDP-11 despite not having seen one.

///
-- 
  Norway is now run by a priest from the fundamentalist Christian People's
  Party, the fifth largest party representing one eighth of the electorate.
-- 
  The purpose of computing is insight, not numbers.   -- Richard Hamming
-------
¹ No, not really.