Haskell int list to String

I would like to know if there is a simple way to turn [5,2,10] into "52a". Where its not just to this case, I want to associate any number >9 with the corresponding letter.

Thanks in advance.

Answers


You want to do something to each element of a list in order to get a new list. In other words, you want to apply a function (that you will have to define yourself) to each element. This is what the map function from the Prelude is for.

To convert between integers and individual characters, you could use the chr and ord functions from the Data.Char module.

So,

map (\i -> if i < 10 then chr (i + ord '0') else chr (i - 10 + ord 'a'))

is a function of type [Int] -> String that does what you want (no error checking included, though).


Slower but more elegant:

f = map ((['0'..'9'] ++ ['a'..'z']) !!)

If your numbers are 0-15 use map intToDigit from Data.Char.


Need Your Help

How to create multiple resources with one Request in Spring Data Rest?

java hibernate jpa spring-data-rest

What do I need to implement in order to create multiple records in my DB with a single http request?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.