Created
January 13, 2015 14:39
-
-
Save uehaj/349950f7a8437644f646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Graphics.Input (..) | |
import Signal | |
import Signal(Channel) | |
import Graphics.Element (..) | |
import Text(..) | |
import List(..) | |
type Keys = Number Int | Add | Sub | Mul | Div | Clear | Enter | |
keys : Channel Keys | |
keys = Signal.channel Clear | |
calculator : (Int, List Int) -> Element | |
calculator (n, xs) = | |
let btn x = button (Signal.send keys x) | |
btnN n = btn (Number n) (toString n) | |
in flow down [ | |
flow right [ btnN 7, btnN 8, btnN 9, btn Add "+" ] | |
, flow right [ btnN 4, btnN 5, btnN 6, btn Sub "-" ] | |
, flow right [ btnN 1, btnN 2, btnN 3, btn Mul "*" ] | |
, flow right [ btnN 0, btn Clear "Clear", btn Enter "Enter", btn Div "/" ] | |
, if (n==0) then (asText xs) else (asText n) | |
] | |
calc : Keys -> (Int, List Int) -> (Int, List Int) | |
calc it (num, xs) = case it of | |
Number n -> (num*10+n, xs) | |
Enter -> (0, num::xs) | |
Clear -> (0,[]) | |
_ -> if (length xs < 2) | |
then (0, xs) | |
else let top = head xs | |
second = head (tail xs) | |
rest = tail (tail xs) | |
in case it of | |
Add -> (0, (second+top) :: rest) | |
Sub -> (0, (second-top) :: rest) | |
Mul -> (0, (second*top) :: rest) | |
Div -> (0, (second // top) :: rest) | |
main=Signal.map calculator (Signal.foldp calc (0,[]) (Signal.subscribe keys)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment