wxhaskell - Keep track of the program variables in Haskell like imperative programs -
i'm having hard time finding out how make change every time user interacts program. it's hard explain here's example (haskell + wxhaskell):
simulate :: int -> frame () -> io () simulate qnr window = fdata <- readfile "qarchive" case (split (listtake (split fdata '\n') qnr 0) '#') of (qst:a:b:c:coralt:answer:x) -> -- gui controls initialization (buttons,text,etc...) nextbutton <- button window [text := "next question ->", on command := set infofield [text := "next question"], position := pt 185 155] return () main :: io () main = start gui gui :: io () gui = window <- frame [text := "question program", clientsize := sz 640 480] headertext <- statictext window [text := "title text", fontsize := 20, position := pt 5 5] simulate 0 window return ()
i want widgets change when "next question" button pressed. want change these widgets values read file. how can keep track of current question number is? cannot increment questionnumber variable, since haskell doesn't permit such things. guess there's way it.
example:
initialize gui read data file if button pressed, increment question number 1 , change widgets.
how tackle kind of problem in functional manner?
the arguments functions variables. user enters new values, pass values functions.
for example, simple program updates value based on user input:
main = loop 0 loop n = print n v <- read `fmap` getline loop (n + v)
note recursive calls 'loop' have different value passed each time, based on user provided.
this fundamental way of thinking in functional programming -- local, mutable variable in loop in imperative program becomes parameter recursive function in functional program.
Comments
Post a Comment