Arc Forumnew | comments | leaders | submitlogin
How to split a string into a list of characters?
2 points by dpkendal 5208 days ago | 2 comments
Hello,

I'm new to Arc and I'd like to split a string into a list of characters to access the car and cdr of the list. Is there a way to do this?

— dpk.



5 points by fallintothis 5207 days ago | link

  arc> (coerce "abc" 'cons)
  (#\a #\b #\c)
  arc> (car (coerce "abc" 'cons))
  #\a
  arc> (cdr (coerce "abc" 'cons))
  (#\b #\c)
coerce works on a wide variety of types. E.g., you can also do

  arc> (coerce (list #\a #\b #\c) 'string)
  "abc"
Keep in mind that certain Arc builtins like map and each will work on strings to begin with.

  arc> (inc #\a)
  #\b
  arc> (inc #\b)
  #\c
  arc> (inc #\c)
  #\d
  arc> (map inc "abc")
  "bcd"

  arc> (each character "abc" (prn character "!"))
  a!
  b!
  c!
  nil

-----

1 point by dpkendal 5204 days ago | link

Thanks. Didn't know coerce could be used like that.

-----