PDA

View Full Version : Need help with Haskell! Can anyone help?


olithompsonuk
08-22-2003, 05:02 PM
Can anyone help? I'm looking for someone that knows even a little about Haskell!!

The problem is: I have a String e.g

LFTWDGTA0012300050Widget

And I need to split it up into four componets like this:-

Stock Code: LFTWDGTA
In Stock: 00123
Reorder Level: 00050
Description: Widget

I need to beable to show all the sections individualy for example
show Stock code, etc.....

I need this to be done in Haskell, not the most popular laguage I know, but it can be written any which way, as long as it kind of works!?!.

I would be greatful for any help on this!
:D

jemfinch
08-22-2003, 05:13 PM
And there are no spaces or anything to separate the components?

Jeremy

Strike
08-22-2003, 07:17 PM
If there are no spaces or other delimiters, just use take and drop. Or you can use my strSlice function to grab a selected number of chars:


strSlice :: Int -> Int -> String -> String
strSlice start end s =
(take (end-start) (drop start s))


Then you can just do something like:

getStockCode = strSlice 0 8
getNumInStock = strSlice 8 13
getReorderNumber = strSlice 13 18
getDescription = drop 18


That should result in the following for that string:

Prelude> getStockCode "LFTWDGTA0012300050Widget"
"LFTWDGTA"
Prelude> getNumInStock "LFTWDGTA0012300050Widget"
"00123"
Prelude> getReorderNumber "LFTWDGTA0012300050Widget"
"00050"
Prelude> getDescription "LFTWDGTA0012300050Widget"
"Widget"

olithompsonuk
08-23-2003, 10:23 AM
Cheers for the code Striker, I have integrated into my program it works brilliantly! Still have a problem though, which is I have put the stock codes into a file and I'm now trying to get an IO prompt from the command line of hugs which will prompt the user for a file name and on entering the file name will open the file with the stock in it. Do you have any ideas? Have spent days on this and still no joy! Thanks oli

sicarius
08-23-2003, 11:52 PM
IO is generally the most iffy part of functional languages. From what I've seen it is a little cleaner in Haskell than some older functional languages.

If you go to haskell.org (http://www.haskell.org) you can download the language report. Just check out the section on the IO classes. You may also want to check out the section on monads.