I keep getting drawn to Haskell’s simplicity, consistency, structural elegance, and perfect typing system, even though it still causes me a great deal of mental strain to understand it. And I found that after being away from it for several months, even the simplest concepts have to be re-learned.
The second thing I did is to give “Programming Praxis” a try. In the past I used Project Euler, but having solved 108+ problems, I’m getting tired of the same old, same old. I’m sure I’ll get to the same point with Praxis, but for now its giving me a change.
Praxis 1 – Create a RPN calculator:
Loved my HP 48sx+ calculator in Engineering. Sad day when it kak’d. Still have trouble using regular algebraic calculators. So this one was is right up my alley. Lots of sample code online, but I started noticing that most code used function composition, rather than using variables. Up until now I always avoided function composition, but it really does make things clearer.
import Data.List solveRPN :: String -> Float solveRPN = head . foldl calcme  . words where calcme (x:y:ys) "*" = (x * y):ys calcme (x:y:ys) "+" = (x + y):ys calcme (x:y:ys) "-" = (y - x):ys calcme (x:y:ys) "/" = (y / x):ys calcme xs numStr = read numStr:xs main = do putStrLn "Enter calculation: " input <- getLine putStrLn $ "Result = " ++ (show $ solveRPN input)