For Programmers: Free Programming Magazines  


Home > Archive > Scheme > December 2004 > converting words to numbers?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author converting words to numbers?
Mephistopheles

2004-12-13, 9:02 pm

Hello,i'm a newbie in scheme :)
I have a bet with my friends.Thus;
I have to write a prog. which will convert words to numbers,for eg:
(convert 'fivethousandthreehundredten)
=> 5310

But i shouldnt use recursion and the word which will be converted must
have
not spaces like five thousand ......

Please help me and i can earn a few bucks from my friends :)

Brian Harvey

2004-12-13, 9:02 pm

"Mephistopheles" <mephistopheles@superonline.com> writes:
>But i shouldnt use recursion


This is sort of like betting you could write the program in Smalltalk
and not use objects: maybe it's possible, but what's the point? You
might as well write it in C.
Mephistopheles

2004-12-13, 9:02 pm

The point is that i must write it in scheme not C.A bet without a border is
meanless.

Pascal Bourguignon

2004-12-13, 9:02 pm

"Mephistopheles" <mephistopheles@superonline.com> writes:

> Hello,i'm a newbie in scheme :)
> I have a bet with my friends.Thus;
> I have to write a prog. which will convert words to numbers,for eg:
> (convert 'fivethousandthreehundredten)
> => 5310
>
> But i shouldnt use recursion and the word which will be converted must
> have
> not spaces like five thousand ......
>
> Please help me and i can earn a few bucks from my friends :)


You could insipire you from this Common Lisp joke of mine, adding a
filter to remove spaces and punctuation:

(defun nl-string-to-number (text)
;; Of course, you could program it more intelligently and more efficiently.
(do* ((i 0 (1+ i))
(s (format nil "~r" i) (format nil "~r" i)))
((or (string-equal s text) (< (* 2 (length text)) (length s)))
(when (string-equal s text) i))))

(defmacro def-s-op (name num-op)
`(defun ,name (&rest args)
(format nil "~r"
(apply (function ,num-op)
(mapcar (function nl-string-to-number) args)))))


(def-s-op +s +)
(def-s-op -s -)
(def-s-op *s *)
(def-s-op /s /)
(def-s-op =s =)
(def-s-op /=s /=)
(def-s-op <s < )
(def-s-op <=s <=)
(def-s-op >s > )
(def-s-op =>s <=)

(-s "one" "two")

--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
Pascal Bourguignon

2004-12-13, 9:02 pm

"Mephistopheles" <mephistopheles@superonline.com> writes:

> The point is that i must write it in scheme not C.A bet without a border is
> meanless.


But since the scheme compiler will remove the tail recusion, you'd
just have to provide your code disassembled to show an iterative
solution. When the border is the universe, it's meaningless all the same.

--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
Mephistopheles

2004-12-13, 9:02 pm

Thanks Pascal for these complicated things :) hmm but i must say it again :
i'm a newbie in scheme.These useful things that you wrote, seems
fantastical far away to me.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com