(List->String) Why isn't it working? 
Author Message
 (List->String) Why isn't it working?

Why doesn't this work?

(define z (list 1 2 3))
(list->string z)

I want to convert a list to a string and I get an error saying that it
expects a Character. What's going on?

--
Johnny Ooi. Aliases: Loopy, Tuxedo Mask, Quote Master.....

WWW             : http://www.*-*-*.com/ ~jjyooi/
ICQ No          : 6155774

"Stay sane guys!"

===============================================================



Tue, 19 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?


Quote:
>Why doesn't this work?

>(define z (list 1 2 3))
>(list->string z)

>I want to convert a list to a string and I get an error saying that it
>expects a Character. What's going on?

From R5RS: "List->string returns a newly allocated string formed from
the characters in the list list, which must be a list of characters."
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1, 2, and 3 are numbers, not characters.  You need to do:

(define z (list #\1 #\2 #\3))
(list->string z) => "123"

Or you could do:

(list->string (map (lambda (num) (string-ref (number->string num) 0))
                   z))

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Tue, 19 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?

Quote:
>Why doesn't this work?

>(define z (list 1 2 3))
>(list->string z)

>I want to convert a list to a string and I get an error saying that it
>expects a Character. What's going on?

I don't understand what you expect. Do you expect it to return the text
string "1 2 3"? That's not what list->string does.

list->string takes a list of characters (integers are not characters) and
returns a text string composed of those characters. For example:

    (string->list "foo")

will give you a list consisting of the characters f, o, and o; and
list->string can turn that list back into the string "foo".

If you're trying to get "1 2 3" out of '(1 2 3), then you may find the
function number->string to be helpful, though you'll still have to figure
out how to apply it to each integer in the list, then concatenate the
strings with spaces inserted between them. Since this is the right time
of year for homework assignments, I think I'll leave it at that...

Craig



Tue, 19 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?

Quote:


> >Why doesn't this work?

> >(define z (list 1 2 3))
> >(list->string z)

> >I want to convert a list to a string and I get an error saying that it
> >expects a Character. What's going on?

> From R5RS: "List->string returns a newly allocated string formed from
> the characters in the list list, which must be a list of characters."
    ...
> Or you could do:

> (list->string (map (lambda (num) (string-ref (number->string num) 0))
>                    z))

Or     (apply string-append (map number->string z))

--

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



Tue, 19 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?

Quote:



> > >Why doesn't this work?

> > >(define z (list 1 2 3))
> > >(list->string z)

> > >I want to convert a list to a string and I get an error saying that it
> > >expects a Character. What's going on?

> > From R5RS: "List->string returns a newly allocated string formed from
> > the characters in the list list, which must be a list of characters."
>     ...
> > Or you could do:

> > (list->string (map (lambda (num) (string-ref (number->string num) 0))
> >                    z))

> Or     (apply string-append (map number->string z))

OIC... I understand now. So if instead of

(define z (list 1 2 3))
(list->string z)

I did something like:

(define z (list #\1 #\2 #\3))
(list->string z)

Would that work?

--
Johnny Ooi. Aliases: Loopy, Tuxedo Mask, Quote Master.....

WWW             : http://www.dcs.qmw.ac.uk/~jjyooi/
ICQ No          : 6155774

"Stay sane guys!"

===============================================================



Fri, 22 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?

Quote:
> If I did something like:

> (define z (list #\1 #\2 #\3))
> (list->string z)

> Would that work?

Wouldn't it be quicker to type that into your Scheme
system and get the answer immediately?

--

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



Fri, 22 Mar 2002 03:00:00 GMT  
 (List->String) Why isn't it working?
I was hoping I could pass a standard list like "list 1 2 3" and convert
that to a string, but now I know otherwise....

Quote:


> > If I did something like:

> > (define z (list #\1 #\2 #\3))
> > (list->string z)

> > Would that work?

> Wouldn't it be quicker to type that into your Scheme
> system and get the answer immediately?

> --

> Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
>          ---  Food, Shelter, Source code.  ---

--
Johnny Ooi. Aliases: Loopy, Tuxedo Mask, Quote Master.....

WWW             : http://www.dcs.qmw.ac.uk/~jjyooi/
ICQ No          : 6155774

"Stay sane guys!"

===============================================================



Sun, 24 Mar 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Why isn't my program working?

2. Why CGI script isn't worked ?

3. question about symbol->string, string->list

4. string->list->string

5. Why isn't Haskell mainstream?---A newbie's view

6. Why doesn't deepcopy work on strings ?

7. Where's the proper list->string ?

8. Why isn't Eiffel as widely used as C++

9. Why isn't Eiffel as widely as C++ (or Smalltalk)

10. Why Forth isn't popular...

11. Why Forth isn't popular...

 

 
Powered by phpBB® Forum Software