0

In Haskell, I got this string "bbbbffff", and I want to get a list in this form:

['b','b','b','b','f','f','f','f']

I think that I can use map, but sincerely, I don't know how to begin, and are a lot of things that I do not understand in Haskell.

Thanks in advance.

3
  • 4
    those two are exactly the same thing in Haskell - the first is syntactic sugar for the second. So you don't have to do anything at all. Commented Aug 24, 2021 at 22:27
  • Thanks for your comment, yes I understand this, but, for example, the string has an undefined length, and I need to compare it with another list, so I need it explicitly in a list form. I don't know if I'm being clear. Commented Aug 24, 2021 at 22:37
  • 2
    Unfortunately you are not being clear to me. There is no such thing as "explicitly in a list form" - unless perhaps you are referring to a string representation? In Haskell a string simply is a list of characters, so you do not need to apply any "conversion function" to get from one to the other. Commented Aug 24, 2021 at 22:42

1 Answer 1

4

By default, a String is already a [Char] (see specification):

A string is a list of characters:

type  String  =  [Char]

They simply don't print as ['b','b','b',...] because [Char] and String is the same type and therefore indistinguishable and must be shown the same way. Indeed, if you input your list you'll see it formatted as a string:

Prelude> 42
42
Prelude> [1,2,3]
[1,2,3]
Prelude> ['b','b','b','b','f','f','f','f']
"bbbbffff"

This means that you can immediately pass it to any list function, without having to do anything with it:

myLength :: [Char] -> Int
myLength (c:rest) = 1 + myLength rest
myLength [] = 0

-- Prints 11
main = print (myLength "hello world")

There are other textual data types like Data.Text, but these are for more advanced use and must be explicitly enabled and used.

Sign up to request clarification or add additional context in comments.

3 Comments

i.e I need to do a ord 'a' + 1 this gave me 98, but I can not do the same with ord "a" + 1
This is because 'a' is a Char and "a" is a [Char]. ord does not work on lists. If you want to do this to each character in the list, you can use map: map (\c -> ord c + 1) "foobar" == [103,112,112,99,98,115]
@Raul I'm happy you got your answer before too long, but note that you only got it because you eventually you posed your actual question in a comment to this answer. Had you put the content of this comment as your actual question you'd probably have got the answer straight away - but the question you asked instead is not related to it so you instead got comments like mine which weren't helpful to you, because I couldn't guess the actual problem. Just something to think about the next time you need to ask something here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.