Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concat two (IO) Strings in Haskell?

Tags:

I know this sound very simple, but I failed to combine two strings into a new one.

The IO String "a" from a gtk entry is fetched by

      a <- (entryGetText text_field) 

The goal is to combine it like:

newstring = "Text: "+a

Any ideas to accomplish that? Thanks!

like image 712
user1415426 Avatar asked May 25 '12 14:05

user1415426


People also ask

How do you concatenate strings in Haskell?

string s = String. Concat(a,b); C#

What does concat do Haskell?

The concatenation of all the elements of a container of lists.

How does Io work in Haskell?

IO is the way how Haskell differentiates between code that is referentially transparent and code that is not. IO a is the type of an IO action that returns an a . You can think of an IO action as a piece of code with some effect on the real world that waits to get executed.


2 Answers

Using string concatenation:

 do a <- entryGetText text_field     let b = "Text:" ++ a     return b 

More simply:

 do a <- entryGetText text_field     return $ "Text:" ++ a 

You can play games too:

 ("Text:" ++) <$> (entryGetText text_field) 
like image 126
Don Stewart Avatar answered Sep 27 '22 18:09

Don Stewart


I believe that in Haskell, the string concatenation operator is ++.

like image 37
Puppy Avatar answered Sep 27 '22 20:09

Puppy