Arc Forumnew | comments | leaders | submitlogin
Map and Strings
2 points by tel 5909 days ago | 2 comments
I know that strings aren't just linked lists, but shouldn't map work over strings?

Viz.

   (all idfn (map is "test" "te"))
   Error: "string-set!: expects type <character> as 3rd argument, given: t; other arguments were: \"\\u0000\\u0000\" 0"


2 points by raymyers 5909 days ago | link

You gave `map' strings as input, so it attempts to return a string. That means it is trying to map the output of `is' into the characters of a string, but t and nil aren't charactors.

  (def char-is (a b) (if (is a b) #\t #\f))
  (map char-is "abba" "abab")  =>  "ttff"
Common Lisp addresses this issue by having `map' take a result type as the first argument.

http://www.lisp.org/HyperSpec/Body/fun_map.html

-----

2 points by ecmanaut 5908 days ago | link

It does:

  (def caesar (char) (coerce (+ (coerce char 'int) 3) 'char))
  #<procedure: caesar>
  (map caesar "caesar")
  "fdhvdu"

-----