Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Haskell list of string problem

Posted on 2010-11-18
6
Medium Priority
?
321 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:SSupreme
  • 3
  • 3
6 Comments
 
LVL 3

Assisted Solution

by:kraiven
kraiven earned 2000 total points
ID: 34163936
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
 
LVL 16

Author Comment

by:SSupreme
ID: 34164075
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
 
LVL 16

Author Comment

by:SSupreme
ID: 34164084
How to add spaces?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
kraiven earned 2000 total points
ID: 34164520
How about:

concatos (x:t) = x ++ " " ++ concatos t  
0
 
LVL 16

Author Closing Comment

by:SSupreme
ID: 34165525
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
 
LVL 3

Expert Comment

by:kraiven
ID: 34165547
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question