Haskell list of string problem

Hello,

What is the best way to define a function that takes a list of words and returns a single piece of text made up the words with a space between each?

Regards
LVL 16
SSupremeAsked:
Who is Participating?
 
kraivenConnect With a Mentor Commented:
How about:

concatos (x:t) = x ++ " " ++ concatos t  
0
 
kraivenConnect With a Mentor Commented:
You probably want the fold operation i.e.

foldr (\acc s -> acc ++ " " ++ s)  "" ["1","2","3","4"]

Open in new window


produces "1 2 3 4 " as its output
0
 
SSupremeAuthor Commented:
Yes, it works.

can I do the same thing with this function?

 
concatos :: [[a]] -> [a]
concatos [] = []
concatos (x:t) = x ++ concatos t

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
SSupremeAuthor Commented:
How to add spaces?
0
 
SSupremeAuthor Commented:
I tried it before and it didn't compile, but I found my mistake and it's compile now.
Type should be: concatos :: [[Char]] -> String
0
 
kraivenCommented:
Gotcha, I hadn't included the function definition in my test otherwise I might have picked up this error also.

Glad it's fixed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.