Solved

Haskell list of string problem

Posted on 2010-11-18
6
306 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 98
matchUp  challenge 9 115
countHi challenge 25 155
object oriented javascript web form 8 147
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 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