From: Kirk S
Subject: convert string to list of symbols
Date: 
Message-ID: <87elt95fyi.fsf@pulsar.wlfdle1.on.wave.home.com>
Hello all.

I need to write a function that does the following.

(foo "abcd") ==> (list 'a 'b 'c 'd)

There are probably some characters in a string that cannot be symbols
(like ' ').  I can safely assume that such chars won't be in the string.
I looked at graham's ACL and browsed the hyperspec but couldn't find the
proper conversion function.

thanks

- kirk

From: Wade Humeniuk
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <9eug2q$8nf$1@news3.cadvision.com>
"abcd" is already a CL _sequence_ of characters.  Why does it need to be a
list?  A list is very much like a _sequence_.

Try things like:

(elt "abcd" 1)

(loop for char across "abcd"
    collect char)

Wade

"Kirk S" <········@yahoo.com> wrote in message
···················@pulsar.wlfdle1.on.wave.home.com...
> Hello all.
>
> I need to write a function that does the following.
>
> (foo "abcd") ==> (list 'a 'b 'c 'd)
>
> There are probably some characters in a string that cannot be symbols
> (like ' ').  I can safely assume that such chars won't be in the string.
> I looked at graham's ACL and browsed the hyperspec but couldn't find the
> proper conversion function.
>
> thanks
>
> - kirk
From: Shannon Spires
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <svspire-283E61.03463929052001@news.nmia.com>
In article <············@news3.cadvision.com>, "Wade Humeniuk" 
<········@cadvision.com> wrote:

> "abcd" is already a CL _sequence_ of characters.  Why does it need to be a
> list?  A list is very much like a _sequence_.
> 
> Try things like:
> 
> (elt "abcd" 1)
> 
> (loop for char across "abcd"
>     collect char)

Or (coerce "abcd" 'list)

Shannon Spires
·······@remove-this.nmia.com
From: Christopher J. Vogt
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <3B12C9A6.F82E3B0A@computer.org>
Kirk S wrote:
> 
> Hello all.
> 
> I need to write a function that does the following.
> 
> (foo "abcd") ==> (list 'a 'b 'c 'd)
> 
> There are probably some characters in a string that cannot be symbols
> (like ' ').  I can safely assume that such chars won't be in the string.
> I looked at graham's ACL and browsed the hyperspec but couldn't find the
> proper conversion function.
> 
> thanks
> 
> - kirk

I don't know of a built-in function that does this, but here is a simple loop:
(loop for i across "foo bar baz" 
      collect (intern (string-upcase i)))
From: Espen Vestre
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <w6g0dpgnji.fsf@wallace.ws.nextra.no>
Kirk S <········@yahoo.com> writes:

> There are probably some characters in a string that cannot be symbols
> (like ' ').  

every string, including " ", can be the name of a symbol! 
-- 
  (espen)
From: Frode Vatvedt Fjeld
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <2hofsdawvx.fsf@dslab7.cs.uit.no>
Kirk S <········@yahoo.com> writes:

> I need to write a function that does the following.
> 
> (foo "abcd") ==> (list 'a 'b 'c 'd)

One possibility in addition to LOOP would be

  (map 'list #'(lambda (c) (intern (string c))) <string>)

-- 
Frode Vatvedt Fjeld
From: Kent M Pitman
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <sfw1yp9xby2.fsf@world.std.com>
Frode Vatvedt Fjeld <······@acm.org> writes:

> Kirk S <········@yahoo.com> writes:
> 
> > I need to write a function that does the following.
> > 
> > (foo "abcd") ==> (list 'a 'b 'c 'd)
> 
> One possibility in addition to LOOP would be
> 
>   (map 'list #'(lambda (c) (intern (string c))) <string>)

Funny, I'd have said:

 (defun foo (string)
   `(list ,@(map 'list 
                 #'(lambda (char) `',(intern (string-upcase char))) 
                 string)))

 (foo "abcd") => (LIST (QUOTE A) (QUOTE B) (QUOTE C) (QUOTE D))

Then again, maybe Kirk didn't express what he was looking for as clearly
as he meant to... ;-)
From: Kirk S
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <8766ek57kh.fsf@pulsar.wlfdle1.on.wave.home.com>
Kent M Pitman <······@world.std.com> writes:

> Frode Vatvedt Fjeld <······@acm.org> writes:
> 
> > Kirk S <········@yahoo.com> writes:
> > 
> > > I need to write a function that does the following.
> > > 
> > > (foo "abcd") ==> (list 'a 'b 'c 'd)
> > 
> > One possibility in addition to LOOP would be
> > 
> >   (map 'list #'(lambda (c) (intern (string c))) <string>)
> 
> Funny, I'd have said:
> 
>  (defun foo (string)
>    `(list ,@(map 'list 
>                  #'(lambda (char) `',(intern (string-upcase char))) 
>                  string)))
> 
>  (foo "abcd") => (LIST (QUOTE A) (QUOTE B) (QUOTE C) (QUOTE D))
> 
> Then again, maybe Kirk didn't express what he was looking for as clearly
> as he meant to... ;-)
> 

Thanks a lot Kent.  This is exactly what I want.  I'm basically making
functions for DFA and NFA-lambda that reads in a character as a symbol not
as a lisp character or a string with just one character.  To write a
function that inputs a word in my DFA/NFA I needed a function that takes a
string and calls my DFA/NFA read symbol function n times converting each
characer to a quote'd symbol like you did in foo.  Maybe it would be
better to read in a character or a string with one character instead of a
symbol?

The way I understand intern now is that if given a string is given to it
that has atleast one lower case character then the string will not eql to
the same string quote'd.

* (eql (intern "abcd") 'abcd)
NIL

but 

* (eql (intern "ABCD") 'abcd)
T

My question is what is the point about distinguishing between case if in
(setf blah "abcd") the case for symbol blah doesn't matter?

* (setf blah "abcd")
"abcd"

* blah
"abcd"

* BLAH
"abcd"

* blAh
"abcd"

What special use does case sensitive symbols returned by intern have?  Why
not just use a string with the same case.

Thank you Kent and Frode for the help.

- kirk
From: Kent M Pitman
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <sfwy9rgm1ve.fsf@world.std.com>
Kirk S <········@yahoo.com> writes:

> My question is what is the point about distinguishing between case if in
> (setf blah "abcd") the case for symbol blah doesn't matter?
> 
> * (setf blah "abcd")
> "abcd"
> 
> * blah
> "abcd"
> 
> * BLAH
> "abcd"
> 
> * blAh
> "abcd"
> 
> What special use does case sensitive symbols returned by intern have?  Why
> not just use a string with the same case.

Symbols are case-sensitive, but the *reader* is case-translating, so you
don't see this.

When READ sees "blah" it upcases it to "BLAH" before calling INTERN.
[However, the reader can be asked not to do this, by appropriately
modifying the readtable-case aspect of the readtable).]

Symbols are maintained case-sensitive because there are myriad applications
where case-sensitive symbols might matter.  A symbol is little more than
an interned string, and string case sometimes matters, so symbol case likewise
sometimes matters.

However, experience with programming languages has shown that most people
don't care about case most of the time in program variable names, so
Lisp does you the favor of making the case mostly irrelevant for program
processing.
From: Frode Vatvedt Fjeld
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <2h4ru4ast0.fsf@dslab7.cs.uit.no>
Kirk S <········@yahoo.com> writes:

> * (eql (intern "abcd") 'abcd)
> NIL
> 
> but 
> 
> * (eql (intern "ABCD") 'abcd)
> T

What you're seeing here is not the effect of INTERN, but rather the
effect of the lisp reader (the R in the REPL interactive loop). The
reader is the component that translates text strings to lisp objects.

By default, the lisp reader upcases symbol-names. One way to override
this behavior is to escape the symbol-name (with the vertical bar
character).

This should demonstrate:

  * 'abcd
  => ABCD  ; the reader up-cased the name

  * '|abcd|
  => abcd  ; no up-casing occurred

  * (intern "abcd")
  => abcd

  (eq (intern "abcd") '|abcd|)
  => t

-- 
Frode Vatvedt Fjeld
From: Gareth McCaughan
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <slrn9h5r7d.1auc.Gareth.McCaughan@g.local>
"Kirk S" wrote:

> I need to write a function that does the following.
> 
> (foo "abcd") ==> (list 'a 'b 'c 'd)

I don't think I believe you. Why do you need to do that?
The only explanation I can think of is that someone has
told you to do it as homework, but it seems an odd thing
to set as homework...

However, here's one way.

  | * (cons 'list (map 'list (lambda (c) `',(intern (string-upcase c))) "foo"))
  |  
  | (LIST 'F 'O 'O)

Note that handing this in as homework, in case you were
contemplating that, is unlikely to be convincing to your
teacher. :-)

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc
From: Kirk S
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <87ae3w58cg.fsf@pulsar.wlfdle1.on.wave.home.com>
················@pobox.com (Gareth McCaughan) writes:

> "Kirk S" wrote:
> 
> > I need to write a function that does the following.
> > 
> > (foo "abcd") ==> (list 'a 'b 'c 'd)
> 
> I don't think I believe you. Why do you need to do that?
> The only explanation I can think of is that someone has
> told you to do it as homework, but it seems an odd thing
> to set as homework...
> 

I'm not lying :).  I wrote some code to process string through DFAs and
NFA-lambdas.  The functions read in symbols like 'a and 'b.  I would like
to have a function that can take a string "abcd" and call my dfa/nfa read
symbol function 4 times with 'a, 'b, 'c, and 'd.

> However, here's one way.
> 
>   | * (cons 'list (map 'list (lambda (c) `',(intern (string-upcase c))) "foo"))
>   |  
>   | (LIST 'F 'O 'O)
> 
> Note that handing this in as homework, in case you were
> contemplating that, is unlikely to be convincing to your
> teacher. :-)
> 

Its true this is for a homework but the assignment doesn't involve
programming.  This is just to verify my answers.

thanks

> -- 
> Gareth McCaughan  ················@pobox.com
> .sig under construc

-- 
From: Gareth McCaughan
Subject: Re: convert string to list of symbols
Date: 
Message-ID: <slrn9h8c6n.2sqt.Gareth.McCaughan@g.local>
"Kirk S" wrote:

> > > I need to write a function that does the following.
> > > 
> > > (foo "abcd") ==> (list 'a 'b 'c 'd)
> > 
> > I don't think I believe you. Why do you need to do that?
> > The only explanation I can think of is that someone has
> > told you to do it as homework, but it seems an odd thing
> > to set as homework...
> 
> I'm not lying :).  I wrote some code to process string through DFAs and
> NFA-lambdas.  The functions read in symbols like 'a and 'b.  I would like
> to have a function that can take a string "abcd" and call my dfa/nfa read
> symbol function 4 times with 'a, 'b, 'c, and 'd.

Why do your functions take symbols rather than characters,
if they're coming from a string? That seems like an odd
design decision.

-- 
Gareth McCaughan  ················@pobox.com
.sig under construc