From: ab talebi
Subject: Primitives for "stings"
Date: 
Message-ID: <3bd20c4b.167275@news.online.no>
strings have a different set of primitive operators than lists. What
is the equvalence of "first" or "car" for stings?

(setf a "strings have a different set of primitives")
(first a) will give me: Error: Not a list
(first (list a)) will give me: "strings have  ...... primitieves"

ab talebi

From: Thomas F. Burdick
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <xcvn12lg6co.fsf@hurricane.OCF.Berkeley.EDU>
············@yahoo.com (ab talebi) writes:

> strings have a different set of primitive operators than lists. What
> is the equvalence of "first" or "car" for stings?
> 
> (setf a "strings have a different set of primitives")
> (first a) will give me: Error: Not a list

FIRST and CAR operate on cons cells.  Strings are arrays, so you can
use operations for arrays:

 * (aref "foo" 0)
 #\f

Or, you can use sequence operations, which work on all sequences
(including strings and lists):

 * (elt "foo" 0)
 #\f

> (first (list a)) will give me: "strings have  ...... primitieves"

Well, of course.  You built a list of one element, your string.  Then,
you retrieved the first element of the list: your string.  LIST
creates a list, it does not coerce something into a list.  In fact,
COERCE can coerce objects of certain types into others, for a fairly
limited but useful set of types:

 * (coerce "foo" 'list)
 (#\f #\o #\o)
 * (first (coerce "foo" 'list))
 #\f

That's probably not what you want to do, though.  I don't find myself
using COERCE very often.  Mostly, I use it for things like:

 (let ((foo (loop for a ...
                  for b ...
                  ...
                  if ...
                    collect ...
                  else ...
                    do ...)))
   (setf foo (frob foo))
   (coerce foo 'vector))

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | No to Imperialist war |                        
     ,--'    _,'   | Wage class war!       |                        
    /       /      `-----------------------'                        
   (   -.  |                               
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Coby Beck
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <KKoA7.380609$aZ.76263323@typhoon.tampabay.rr.com>
"ab talebi" <············@yahoo.com> wrote in message
····················@news.online.no...
> strings have a different set of primitive operators than lists. What
> is the equvalence of "first" or "car" for stings?
>
> (setf a "strings have a different set of primitives")
> (first a) will give me: Error: Not a list
> (first (list a)) will give me: "strings have  ...... primitieves"
>

Check
http://www.xanalys.com/software_tools/reference/HyperSpec/Body/chap-16.html

You may find a hint if you click the "concepts" link.

Coby
--
(remove #\space "coby . beck @ opentechgroup . com")
From: Pierre R. Mai
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <87sncdssx6.fsf@orion.bln.pmsf.de>
············@yahoo.com (ab talebi) writes:

> strings have a different set of primitive operators than lists. What
> is the equvalence of "first" or "car" for stings?

Strings have some of their own accessors, like char:

(char "silly-string" 0) => #\s

Strings are also vectors/one-dimensional arrays, and as such the
vector/array accessors also apply:

(aref "silly-string" 0) => #\s

Furthermore vectors and lists are sequences, and hence share the
sequence accessors:

(elt "silly-string" 0) => #\s
(elt '(#\s #\i #\l #\l #\y) 0) => #\s

Note though that none of these is the equivalent of car, since car is
logically an operation on conses, not lists (though lists are built
out of conses in a certain way).  They are to some extent the
equivalent of first.

BUT there is no equivalent of REST or even CDR for arrays, for fairly
obious reasons.

In any case you really should get yourself the HyperSpec, and a good
tutorial, which will answer such questions fairly well.

Regs, Pierre.

-- 
Pierre R. Mai <····@acm.org>                    http://www.pmsf.de/pmai/
 The most likely way for the world to be destroyed, most experts agree,
 is by accident. That's where we come in; we're computer professionals.
 We cause accidents.                           -- Nathaniel Borenstein
From: ab talebi
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <3bd2c087.428654@news.online.no>
On 21 Oct 2001 03:06:29 +0200, "Pierre R. Mai" <····@acm.org> wrote:

<.....>

>BUT there is no equivalent of REST or even CDR for arrays, for fairly
>obious reasons.
>

Yes, I think I'll just have to think of another approach because I
really need Car, and Rest

How can we use the Read-Line function and end up with a list instead
of a string?

ab talebi
From: Erik Naggum
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <3212658210473535@naggum.net>
* ············@yahoo.com (ab talebi)
| Yes, I think I'll just have to think of another approach because I
| really need Car, and Rest

  It looks like you are thinking in C, where incrementing a pointer
  effectively gives you rest for vectors (one-dimensional arrays).

| How can we use the Read-Line function and end up with a list instead
| of a string?

  See the function coerce.

  Which books and other resources are you using to study Common Lisp?

///
-- 
  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
From: Kent M Pitman
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <sfwn12los7r.fsf@world.std.com>
············@yahoo.com (ab talebi) writes:

> On 21 Oct 2001 03:06:29 +0200, "Pierre R. Mai" <····@acm.org> wrote:
> 
> <.....>
> 
> >BUT there is no equivalent of REST or even CDR for arrays, for fairly
> >obious reasons.
> >
> 
> Yes, I think I'll just have to think of another approach because I
> really need Car, and Rest
> 
> How can we use the Read-Line function and end up with a list instead
> of a string?

(defun khar (string) (char string 0))
(defun khdr (string) (subseq string 1))
From: Steve Long
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <3BD354C3.B55402A1@isomedia.com>
Kent M Pitman wrote:

> (defun khar (string) (char string 0))
> (defun khdr (string) (subseq string 1))

These are, of course, pronounced with that "kh" sound common to names like
"Khomeini." :)
From: Kent M Pitman
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <sfw6698ps66.fsf@world.std.com>
Kent M Pitman <······@world.std.com> writes:

> (defun khar (string) (char string 0))
> (defun khdr (string) (subseq string 1))

Btw, in the T dialect of Scheme (Yale Scheme), if I recall correctly,
we used to call these operations char and chdr.  The index argument 
to char was optional.  But I Jonatahn Rees, who was implementing it,
arranged chdr (or tried to) so that that you could step a byte pointer
in a string without, er, chonsing.  The idea was that C or assembly code
commonly did this and we wanted a way to express the same in Lisp, plus
also people were used to passing a single argument in recursions to
step down lists and we wanted the same for strings.  I was only around
for the first few months of the T project so I didn't get to see how this
idea played out in practice--I could ask Jonathan, or maybe someone reading
this (I know Drew McDermott is out there posting now and again for example)
remembers if it worked right.  But I thought it a noble goal in any case.
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.
From: Erik Naggum
Subject: Re: Primitives for "stings"
Date: 
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.
From: Kalle Olavi Niemitalo
Subject: Re: Primitives for "stings"
Date: 
Message-ID: <87y9m5vydm.fsf@Astalo.y2000.kon.iki.fi>
············@yahoo.com (ab talebi) writes:

> How can we use the Read-Line function and end up with a list instead
> of a string?

You can first read the string and then generate a list that
contains the same characters:

  (coerce "Hello there!" 'list)
  => (#\H #\e #\l #\l #\o #\Space #\t #\h #\e #\r #\e #\!)