在命令式程序中跟踪Haskell中的程序变量 [英] Keep track of the program variables in Haskell like imperative programs

查看:125
本文介绍了在命令式程序中跟踪Haskell中的程序变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次用户与我的程序进行交互时,我都很难找出如何改变内容。这很难解释,所以这里有一个例子(Haskell + wxhaskell):

  simulate :: Int  - > Frame() - > IO()
模拟qNr窗口= do
fdata< - readFileqarchive
case(split(listTake(split fdata'\\\
')qNr 0)'#')of
(qst:a:b:c:coralt:answer:x) - >
- 按钮窗口[text:=Next Question - >,on command:= set infoField [text :=Next Question],position:= pt 185 155]
return()

main :: IO()
main =启动gui

gui :: IO()
gui = do
window< - frame [text:=Question program,clientSize:= sz 640 480]
headerText< - staticText窗口[text:=Title Text,fontSize:= 20,position:= pt 5 5]
模拟0窗口
return()

我想在按下Next Question按钮时更改一些小部件。我想将这些小部件更改为从文件读取的某些值。我如何跟踪当前问题编号是什么?我实际上不能将questionNumber作为变量增加,因为Haskell不允许这样的事情。



例子:

 我猜这是另一种方式。初始化GUI 
从文件
读取数据如果按下按钮,则将问题编号加1并更改小部件。

您如何以功能性的方式解决这类问题?

解决方案

函数的参数是你的变量。当用户输入新值时,将这些值传递给函数。



例如,一个简单的程序,根据用户输入更新值:

  main = loop 0 

loop n = do
print n
v< - read`fmap` getLine
loop(n + v)

请注意,递归调用'loop'根据用户提供的内容,每次都传递不同的值。

这是函数式编程思维的基本方法 - 在一个函数式编程中,什么是局部可变变量在命令式程序中循环成为函数式程序中的递归函数的参数。

I'm having a hard time finding out how to make something change every time the user interacts with my program. It's hard to explain so here's an example (Haskell + wxhaskell):

simulate :: Int -> Frame () -> IO ()
simulate qNr window = do
 fdata <- readFile "qarchive"
 case (split (listTake (split fdata '\n') qNr 0) '#') of
  (qst:a:b:c:coralt:answer:x) -> do
   -- 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 = do
 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 some widgets to change when the "Next Question" button is pressed. I want to change these widgets to some values I read from a file. How can I keep track of what the current question number is? I cannot actually increment questionNumber as a variable, since Haskell doesn't permit such things. I guess there's another way to do it.

Example:

Initialize GUI
Read data from file
If button is pressed, increment question number by 1 and change widgets.

How do you tackle this kind of problem in a functional manner?

解决方案

The arguments to functions are your variables. As the user enters new values, pass those values to functions.

For example, a simple program that updates a value based on user input:

main = loop 0

loop n = do
    print n
    v <- read `fmap` getLine
    loop (n + v)

Note the recursive calls to 'loop' have a different value passed each time, based on what the user provided.

This is a fundamental way of thinking in functional programming -- what would be a local, mutable variable in a loop in an imperative program becomes a parameter to a recursive function in a functional program.

这篇关于在命令式程序中跟踪Haskell中的程序变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆