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. Thanx for your time and help. _________________________________________________________________ Hot chart ringtones and polyphonics. Go to http://ninemsn.com.au/mobilemania/default.asp
On Mon, 18 Aug 2003, Trouble ... wrote:
How do I translate a string into an integer.
You can use the overloaded function read for this... read "4" would give 4 This function will convert any string into any value (as long as the string is properly formatted and the value implements the function), BUT this assumes that there is a way for haskell to figure out WHAT type of value you wish to read from the string. Most of the time the way you use the resulting value will be more than enough for Haskell to figure out that it is indeed an Int that is supposed to be read, otherwise you'll have to explicitly write out the type signature of that function. -- ---------------------------------------- | Sebastian Sylvan | | ICQ: 44640862 | | Tel: 073-6818655 / 031-812 817 | | | | | | Hard Work Often Pays Off After Time | | But Laziness Always Pays Off Now! | ----------------------------------------
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.)
participants (3)
-
Derek Elkins -
Sebastian Sylvan -
Trouble ...