From 78001436a52a4cc9b5e22bca489fb8ed09a72376 Mon Sep 17 00:00:00 2001 From: Raymond Hogenson Date: Thu, 17 May 2018 09:42:44 -0800 Subject: Write a calculator It's pretty nice. It works for what I want. I don't think I have any complaints about its functionality. I'll add new functions as I need them. Haskell is actually really easy to program in. I wonder actually if this would have been easier in Python. I think no. --- main.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 main.hs (limited to 'main.hs') diff --git a/main.hs b/main.hs new file mode 100644 index 0000000..3c3d457 --- /dev/null +++ b/main.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Graphics.UI.Gtk +import qualified Control.Monad.IO.Class as Cl +import qualified RealCalc + +main :: IO () +main = do _ <- initGUI + w <- windowNew + windowSetKeepAbove w True + set w [ windowDecorated := False, windowTypeHint := WindowTypeHintDock, windowResizable := False ] + grid <- gridNew + input <- entryNew + output <- labelNew (Nothing :: Maybe String) + gridAttach grid input 0 0 1 1 + gridAttach grid output 0 1 1 1 + containerAdd w grid + _ <- w `on` deleteEvent $ do + Cl.liftIO mainQuit + return False + _ <- input `on` keyReleaseEvent $ do + k <- eventKeyName + let quit = Cl.liftIO mainQuit + case k of + "Escape" -> quit + "Return" -> quit + _ -> return () + s <- Cl.liftIO $ entryGetText input + case RealCalc.calculate s of + Nothing -> return () + Just r -> Cl.liftIO $ labelSetText output r + return False + widgetShowAll w + mainGUI -- cgit v1.3.1