string function returns literal list

Q&A's, tips, howto's

string function returns literal list

Postby sigipa » Wed Oct 19, 2016 1:43 am

Hello All,

When the string function is applied to a list, it returns a literal list. See code below.

(string '("a" "b" "c"))

This returns: ("a" "b" "c")

I expected it to return: "abc"

Is this working as intended? If so, is there another function that will return a string without the parens and quotes?

Thanks,
-S
sigipa
 
Posts: 6
Joined: Fri Jan 30, 2015 4:06 am

Re: string function returns literal list

Postby steloflute » Wed Oct 19, 2016 4:13 am

It is intended. See http://www.newlisp.org/downloads/newlisp_manual.html#string

Translates into a string anything that results from evaluating exp-1—. If more than one expression is specified, the resulting strings are concatenated.

Code: Select all
(string 'hello)          → "hello"
(string 1234)            → "1234"
(string '(+ 3 4))        → "(+ 3 4)"
(string (+ 3 4) 8)       → "78"
(string 'hello " " 123)  → "hello 123"


I suggest using apply as follows:
Code: Select all
> (apply string '("a" "b" "c"))
"abc"
steloflute
 
Posts: 13
Joined: Tue Nov 06, 2012 4:02 pm

Re: string function returns literal list

Postby sigipa » Wed Oct 19, 2016 12:24 pm

Hi,

Works perfectly. Thanks.

-S
sigipa
 
Posts: 6
Joined: Fri Jan 30, 2015 4:06 am

Re: string function returns literal list

Postby kesha » Fri Nov 11, 2016 1:04 am

Hi,
http://www.newlisp.org/downloads/newlisp_manual.html#join

Code: Select all
> (set 'MYLIST '("a" "b" "c"))
("a" "b" "c")
> (join MYLIST)
"abc"
> (join MYLIST "-")
"a-b-c"
> (join MYLIST (string "-" (join MYLIST) "-"))
"a-abc-b-abc-c"
kesha
 
Posts: 7
Joined: Tue Dec 04, 2012 9:11 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron