Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create List of Strings in Haskell

Tags:

haskell

I am making the pilgrimage from Java to Haskell. Broadly speaking, I get the main concepts behind Haskell. Reading all the tutorials and books 'makes sense' but I am getting stuck writing my own code from scratch.

I want to create 1000 files on the file system with names

"myfile_1.txt" ... "myfile_1000.txt"

and each containing some dummy text.

so far I have worked out the whole IO thing, and realise I need to build a list of Strings 1000 elements long. So I have:

buildNamesList :: [] -> []
buildNamesList ???

Once I have the List I can call the writefile method on each element. What I can't figure out is how to add a number to the end of a String to get each fileName because I can't have an int i = 0, i ++ construct in Haskell.

I am a bit out of my depth here, would appreciate some guidance, thanks

like image 216
sectornitad Avatar asked Jan 29 '26 12:01

sectornitad


1 Answers

One possible solution:

buildNamesList = map buildName [1..1000]
  where buildName n = "myfile_" ++ show n ++ ".txt"
like image 181
Arjan Avatar answered Jan 31 '26 06:01

Arjan