From: Murthy S Gandikota
Subject: Is MAP meant to operate on strings?
Date: 
Message-ID: <1991Feb23.205613.14321@magnus.ircc.ohio-state.edu>
This could be a naive question, but what the heck I will go ahead.

(map 'string #'(lambda (x) #\h) "replace every char with h")

==> "hhhhhhhhhhhhhhhhhhhhhhhhh"

Whereas if I try to do

(map 'string #'(lamda (x) (if (equal x #\e) #\f)) "I want to replace e
with f")

==> Error:  ARG not a character 

(this is all the error informatin my LISP machine displays)

Can anyone point out why the error happens?

(BTW, I have tried another way which uses an explict do loop and
checks: (string= (string (char x i)) "e") and then replaces using
(setf (char x i) "f") where x is the input string and i is the index,
i.e. the position inside the string. Also, the DOTIMES needs to loop
(LENGTH x) times to make the substitution, so after all, it is not
that bad. But, I still wonder what's wrong with the using MAP)


Thanks

Murthy Gandikota
From: Barry Margolin
Subject: Re: Is MAP meant to operate on strings?
Date: 
Message-ID: <1991Feb23.220923.10748@Think.COM>
In article <······················@magnus.ircc.ohio-state.edu> ······@magnus.ircc.ohio-state.edu (Murthy S Gandikota) writes:
>(map 'string #'(lamda (x) (if (equal x #\e) #\f)) "I want to replace e
>with f")
>
>==> Error:  ARG not a character 
>
>(this is all the error informatin my LISP machine displays)

Must not be a Symbolics Lisp Machine.  After I fix the misspelling of
"lambda", mine says:

Trap: The first argument given to the SYS:CHAR-LDB-INTERNAL instruction, NIL,
      was not a character.

The problem is that your function returns NIL when the character isn't #\e,
and NIL isn't a valid element of a string.  You should have it return its
argument:

	#'(lambda (x) (if (char-equal x #\e) #\f x))

By the way, you might want to replace this with SUBSTITUTE:

	(substitute #\f #\e "I want to replace e with f")
--
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar