[Haskell-beginners] string instead of char

Michael Snoyman michael at snoyman.com
Sun Oct 4 15:14:05 EDT 2009


Or instead of ['a', 'e', 'i', 'o', 'u'] use "aeiou". However, I think the
case statement, while my verbose, is also clearer. But that's purely a style
choice.

Michael

On Sun, Oct 4, 2009 at 8:55 PM, Robert Ziemba <rziemba at gmail.com> wrote:

> You can also do something like this instead of the case statement (this is
> using GHCI).
> Prelude> let exclude = ['a', 'e', 'i', 'o', 'u']
> Prelude> let f excl letter | letter `elem` excl = 'x' | otherwise = letter
> Prelude> map (f exclude) "hello"
> "hxllx"
>
> Rob Ziemba
>
> On Sun, Oct 4, 2009 at 9:10 AM, Colin Paul Adams <colin at colina.demon.co.uk
> > wrote:
>
>> >>>>> "John" == John Moore <john.moore54 at gmail.com> writes:
>>
>>    John> Hi, I am now writing a function that replaces vowels with
>>    John> the letter x.  eg.put in a string "help" and out comes hxlp.
>>    John> I tried this:
>>
>>    John> f x = case x of
>>
>>    John>       {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' ->
>>    John> 'x';_ -> x}
>>
>>    John> but this wont work on strings, only on the individual
>>    John> letters. Any direction would be very welcome.
>>
>> You need to call
>>
>> map f "whatever"
>>
>> where f is your function for replacing a character (as above).
>> --
>> Colin Adams
>> Preston Lancashire
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091004/8bdcbff0/attachment.html


More information about the Beginners mailing list