{-
Man, this board is slow. Here, have a calculator.
very minimalist, but i think it can do most things.
of course it's horribly verbose, but writing a real
parser = pain. the code is very simple and elegant
imo
-}
needs more "echo $[MATH_EXPRESSION_GOES_HERE]" amirite?
Name:
Anonymous2005-07-10 15:24
eval made smaller
eval = foldl f [] where
f ys (Num n) = n:ys
f (n:ys) Sin = sin n:ys
f (m:n:ys) Add = m+n:ys
f (m:n:ys) Min = m-n:ys
f (m:n:ys) Mul = m*n:ys
f (m:n:ys) Div = m/n:ys
f ys _ = ys
Name:
Anonymous2005-07-10 15:25
here, calc = eval . parse . words
Name:
Anonymous2005-07-10 15:30
i'm done
evalparse = foldl f [] where
f (m:n:ys) "+" = m+n:ys
f (m:n:ys) "-" = m-n:ys
f (m:n:ys) "*" = m*n:ys
f (m:n:ys) "/" = m/n:ys
f (n:ys) "sin" = sin n:ys
f ys x = (read x :: Double):ys
calc = evalparse . words
Name:
Anonymous2005-07-10 17:25
orz...i fucked up the evaluation order
Name:
Anonymous2005-07-10 19:14
Prefix/postfix are for sick people
You can do all that crap in a Perl script with just one line (Windows) or two lines (*ix).
Name:
Anonymous2005-07-10 21:14
I must be sick then, because I love RPN.
Until you've used a stack-based calculator that uses RPN, like some of HP's older calculators, you just can't appreciate its wonders. Infix notation frankly stinks for doing anything other than sitting in a book.
nice, although mine now has variables that can store arbitrary expressions. good luck doing that in rpn!
Name:
Anonymous2005-07-11 5:52
>>15
Well you could do that with RPN. Something like this:
3 6 + STORE a
a 45 / =====> 0.2
Name:
Anonymous2005-07-11 5:58
Storing functions:
[3 +] STORE f
34 f ====> 37
Name:
Anonymous2005-07-11 8:35
MORE LIKE f+=3 AM I RITE
Name:
Anonymous2005-07-11 11:05
>>15
- My poor old calc can do that too. ;_;
- RPN doesn't prevent that. Hell, that's almost the basis of Forth. Lisp does it too, albeit prefix instead of postfix.