Solved

Haskell list of string problem

Posted on 2010-11-18
6
303 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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
kraiven earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countXY challenge 28 146
tenRun challenge 28 93
canBalance challenge 34 70
Python multiple IF statements 4 76
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now