4 Replies - 2492 Views - Last Post: 03 August 2010 - 11:01 AM

#1 monkeyC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-July 10

Haskell: file as string, pattern matching overlapping patterns?

Posted 29 July 2010 - 09:01 AM

SUMMARY OF PROBLEM: textfile with multiple lines shall be partly replicated, partly cut and rearranged. Only constant non-varying content is in beginning and end of line, in between, content is differing, also in length. How to pattern match textfile to variables?

In detail:

Quote

table bullshit
values 'blah blah blah';
values 'blah megablah';
values 'blah blah blahhhh';
nexttable 'something else'
values 'blah blah blaaaah';


Should be converted to:

Quote

table bullshit values 'blah blah blah'
table bullshit values 'blah megablah'
table bullshit values 'blah blah blahhhhhh'
nexttable more BS values 'blah blah bllllaah'


And so on, inserting the table-line in front of every other line.

My problem:
1. I can't use hGetLine with recursion, because I have to add different lines together. Thus I need to put the whole stuff together as a string with hGetContents
2. I can't properly define the parts of the whole textfile as a string, to play around with, as each sentence has varying lengths. Pattern matching with (table, _, _, _, values) is therefore not possible.

I thought of this:
myConvert::[Char] -> [Char]                                                       
myConvert wholesentence firstpart value = wholesentence ++ init firstpart ++ value            
 where head wholesentence = "table"                                                           
       last tablevalue    = ";"                                                               
       head firstpart     = "table"                                                           
       last firstpart     = "values"                                                          
       head value         = "values"                                                          
       last value         = ";"         


This function put in a recursion IO, would go through my entire string matching:

table bullshit values blah2 values blah3 values blah4

changing it to

table bullshit values blah1, table bullshit values blah2, values blah3

starting over again, just hopping one "blah" further:

table bullshit values blah2, values blah3 values blah4 -->
table bullshit values blah2 table bullshit values blah3, values blah4

This doesn't work, as the definitions in my where clause are overlapping; apparently, input can only be matched to function variables exclusively, not twice. Any idea of what tricks I can use to get around this?

This post has been edited by monkeyC: 29 July 2010 - 09:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Haskell: file as string, pattern matching overlapping patterns?

#2 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Haskell: file as string, pattern matching overlapping patterns?

Posted 30 July 2010 - 07:47 AM

Well, the problem is that what you're doing isn't anywhere near what you are trying to do.

When you define those where clauses, you're defining a function head that takes a wholesentence, a function last that takes a tablevalue, and so on. where clauses are just ways to define functions (which in Haskell, are just normal values) locally. The problem is that these definitions are overlapping, and these functions are already defined.

I can't for the life of me understand what exactly you're trying to accomplish with this where clause. It isn't because you didn't explain things properly, and more related to the fact that I'm not so great with Haskell anymore.

I think what you want is probably going to be some cases and ifs.

Maybe if you can tell me what you *thought* you were doing with the where, I may be able to follow up with more info.
Was This Post Helpful? 1
  • +
  • -

#3 monkeyC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-July 10

Re: Haskell: file as string, pattern matching overlapping patterns?

Posted 01 August 2010 - 12:23 PM

I was really stuck when I wrote the first post, so I even made a few typos that obfuscated the whole thing further. I decided to check out pattern matching from a 3rd and 4rth source, and I have come a little further. I think it will get very messy if I start dwelling upon what I thought about doing with overlapping matching, and pull you into that quagmire. I mixed up line thought with the whole file as a string thought, to put it short. I am looking at nested case structures with recursion at the branches and maybe I can find something sweet there, if not, I'll come crying again. ^^
Was This Post Helpful? 0
  • +
  • -

#4 monkeyC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 14-July 10

Re: Haskell: file as string, pattern matching overlapping patterns?

Posted 03 August 2010 - 04:37 AM

Turns out, this problem is way ahead of me. Read the stickies here on where to get help, and was offered some nice input on stackoverflow: http://stackoverflow...variable-length

Using an already existing parsing tool seems to be the way to go. Maybe I will figure out how to write my own, when I get more skill. ^^
Was This Post Helpful? 0
  • +
  • -

#5 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Haskell: file as string, pattern matching overlapping patterns?

Posted 03 August 2010 - 11:01 AM

Haskell has some excellent parsing tools. I guess I should have mentioned them. Real World Haskell has an entire chapter on Parsec. It might be helpful to you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1