help!

Derek Elkins ddarius@hotpop.com
Sun, 17 Aug 2003 21:32:12 -0400


On Mon, 18 Aug 2003 10:03:45 +1000
"Trouble ..." <troublesome_girl@hotmail.com> wrote:

> Hi!
> 
> I'm new to this whole programming thing, and I was wondering if
> someone can help me out with the following question:
> 
> How do I translate a string into an integer. For example, I need to
> know how to translate "one hundred" into 100, or "fifty thousand  ten
> " into 50 010 etc etc .. I need to know how to do this for single
> numbers, hundreds, thousands and millions using Haskell.

This sounds like homework, nevertheless here's something to get you
started.

You can use the standard function 'words' to turn the string into a list
of words, e.g. words "foo bar baz" ==> ["foo","bar","baz"]

Then you can use some kind of mapping function/data structure (e.g.
FiniteMap, assoc list, a function that pattern matches each possibility)
to map the words to numbers, so you'd have something like,
mappingFunc ["one","hundred"] ==> [1,100]

Then with a little thinking and trial & error an algorithm to combine
those numbers correctly isn't too hard.  Exploring the standard
libraries is helpful for this (or heck in general, there are all kinds
of good things in there).

Beyond this, you'd probably want some error checking, "three three" or
"thirteen million two million" aren't numbers, and it's a simple
exercise in abstracting out repeated patterns to generalize to the
largest multiple you have (i.e. to handle billions, trillions, etc.)