Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split lines in Haskell?

I have made a program which takes a 1000 digit number as input. It is fixed, so I put this input into the code file itself. I would obviously be storing it as Integer type, but how do I do it?

I have tried the program by having 1000 digits in the same line. I know this is the worst possible code format! But it works.

How can assign the variable this number, and split its lines. I read somewhere something about eos? Ruby, end of what?

I was thinking that something similar to comments could be used here.

Help will be appreciated.

the basic idea is to make this work:

a=3847981438917489137897491412341234
983745893289572395725258923745897232

instead of something like this:

a=3847981438917489137897491412341234983745893289572395725258923745897232
like image 267
Abcd Avatar asked Aug 31 '12 18:08

Abcd


1 Answers

Haskell doesn't have a way to split (non-String) literals across multiple lines. Since Strings are an exception, we can shoehorn in other literals by parsing a multiline String:

v = read
    "32456\
    \23857\
    \23545" :: Integer

Alternately, you can use list syntax if you think it's prettier:

v = read . concat $
    ["32456"
    ,"24357"
    ,"23476"
    ] :: Integer

The price you pay for this is that some work will be done (once) at runtime, namely, the parsing (e.g. read).

like image 144
Daniel Wagner Avatar answered Nov 13 '22 18:11

Daniel Wagner