Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char to string function

Tags:

haskell

I have a very simple question: Given a function accepting a char and returning a string

test :: Char -> [String] 

how can one convert the char into a string? I'm confused over the two types.

like image 259
Lunar Avatar asked May 29 '11 16:05

Lunar


People also ask

What is charAt () in Java?

The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.

How do I convert a char to a string?

We can convert a char to a string object in java by using the Character. toString() method.

Can you use toString on char?

The toString() and valueOf() methods can both be used to convert a char to a string in Java.


1 Answers

In Haskell String is an alias for [Char]:

type String = [Char] 

If you just want a function that converts a single char to a string you could e.g. do

charToString :: Char -> String charToString c = [c] 

If you prefer pointfree style you could also write

charToString :: Char -> String charToString = (:[]) 
like image 127
Thies Heidecke Avatar answered Sep 20 '22 05:09

Thies Heidecke