3pIntro.hs

-- | GUI programming in a web browser
-- Examples introducing threepenny-gui
-- Functional Programming course 2018.
-- Thomas Hallgren

{-
This started as a skeleton, the definitions were filled in
during the lecture.
-}

import Graphics.UI.Threepenny

main :: IO ()
main = startGUI config gui

gui :: Window -> UI ()
gui w =
  do getBody w #+ [h1 # set text "Hello world!"]
               # set style [("background","lightskyblue")]
     disp <- mkElement "input" # set value "0"
     getBody w #+ [element disp]
     let addButton :: String -> (Int->Int) -> UI ()
         addButton label f =
           do btn <- mkElement "button" # set text label
              getBody w #+ [element btn]
              on click btn (\ _ -> do s <- get value disp
                                      let old = read s :: Int
                                          new = f old
                                      set' value (show new) disp)
     addButton "+1" (+1)
     addButton "-1" (subtract 1)
     addButton "*2" (*2)
     addButton "0" (const 0)
     return ()



config = defaultConfig { jsLog= \ _ -> return ()}

Plain-text version of 3pIntro.hs | Valid HTML?