在 haskell 中将 TUI 转换为 GUI

发布于 2024-11-14 17:47:24 字数 1448 浏览 1 评论 0原文

我正在尝试将 Haskell 程序转换为 Haskell GUI 程序, 但由于我是 Haskell 的新手, 每次我尝试某件事时都会遇到很多错误。 我在 Stack Overflow 上多次询问这个程序, 但每当一个错误消失时,就会出现两个错误。

很抱歉问类似的问题,但是 我打算转换的程序的能力是 非常简单的单词搜索。 接收输入字符串,搜索单词,在窗口上打印。

任何建议、提示或例子都会对我非常有帮助。

我使用的是 Windows XP。抱歉,代码非常糟糕。

--GUI routine
import Graphics.UI.Gtk
import Text.Regex.Posix ((=~))
import Control.Monad (when)
--core routine
matchWord :: String -> String -> Int
matchWord file word = length . filter (== word) . concat $ file =~ "[^- \".,\n]+"

--main start
main :: IO ()
main =
      do initGUI
         win <- windowNew
         windowSetTitle win "WORD SEARCHER"
         win `onDestroy` mainQuit

         fch <- fileChooserWidgetNew FileChooserActionOpen
         containerAdd win fch 

         targetFile <- fileChooserGetFilename fch --wrong?

         ent <- entryNew
         btn <- buttonNewWithLabel "Click to search"
         st <- labelNew $ Just "Found : 0      "

         col <- vBoxNew False 5
         containerAdd col ent
         containerAdd col btn
         containerAdd col st    

         btn `onClicked` do targetWord <- entryGetText ent
                            fileData <- readFile Just targetFile
                            found <- matchWord fileData targetWord
                            labelSetText st found
         containerAdd win col
         widgetShowAll win
         mainGUI

感谢您的阅读

I am trying to convert a Haskell program to a Haskell GUI program,
but since I am very very new at Haskell,
every time I try something I get lots of errors.
I asked on Stack Overflow many time for this program,
but whenever an error disappears, two errors arise.

Sorry for asking similar question, but
the program's ability what I intend to convert is
very simple word searching.
Receive input string, search the word, print on window.

Any advice, hint or example would be very helpful for me.

I am on Windows XP. Sorry for very poor code.

--GUI routine
import Graphics.UI.Gtk
import Text.Regex.Posix ((=~))
import Control.Monad (when)
--core routine
matchWord :: String -> String -> Int
matchWord file word = length . filter (== word) . concat $ file =~ "[^- \".,\n]+"

--main start
main :: IO ()
main =
      do initGUI
         win <- windowNew
         windowSetTitle win "WORD SEARCHER"
         win `onDestroy` mainQuit

         fch <- fileChooserWidgetNew FileChooserActionOpen
         containerAdd win fch 

         targetFile <- fileChooserGetFilename fch --wrong?

         ent <- entryNew
         btn <- buttonNewWithLabel "Click to search"
         st <- labelNew $ Just "Found : 0      "

         col <- vBoxNew False 5
         containerAdd col ent
         containerAdd col btn
         containerAdd col st    

         btn `onClicked` do targetWord <- entryGetText ent
                            fileData <- readFile Just targetFile
                            found <- matchWord fileData targetWord
                            labelSetText st found
         containerAdd win col
         widgetShowAll win
         mainGUI

thank you for reading

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-11-21 17:47:24

这将帮助您开始。

targetFile <- fileChooserGetFilename fch

此时,targetFile 的类型为 Maybe String;也就是说,它将返回 Just "somestring"Nothing。您需要 "somestring" 部分(如果可用)。您可以通过模式匹配来获取它:

Just targetFile <- fileChooserGetFilename fch

如果fileChooserGetFilename的结果返回Nothing,这将失败并显示不透明的错误消息。为了提高鲁棒性,您可以对结果进行案例分析:

maybeTargetFile <- fileChooserGetFilename fch
targetFile <- case maybeTargetFile of
                  Nothing -> fail "I need a filename!"
                  Just file -> return file

另一个问题在这一行:

found <- matchWord fileData targetWord

x <- m 用于将操作 m 的结果绑定到变量中x,但 matchWord 返回一个 Int,而不是一个操作(例如,IO a 对于某些 a )。

This will get you started.

targetFile <- fileChooserGetFilename fch

At this point, targetFile has type Maybe String; that is, it will return either Just "somestring" or Nothing. You want the "somestring" part, if it's available. You can get it by pattern matching:

Just targetFile <- fileChooserGetFilename fch

This will fail with an opaque error message if the result of fileChooserGetFilename returned Nothing. For more robustness you can case analyse the result:

maybeTargetFile <- fileChooserGetFilename fch
targetFile <- case maybeTargetFile of
                  Nothing -> fail "I need a filename!"
                  Just file -> return file

The other problem is in this line:

found <- matchWord fileData targetWord

x <- m is used to bind the result of an action m into the variable x, but matchWord returns an Int, not an action (eg. IO a for some a).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文