在 haskell 中将 TUI 转换为 GUI
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将帮助您开始。
此时,
targetFile
的类型为Maybe String
;也就是说,它将返回Just "somestring"
或Nothing
。您需要"somestring"
部分(如果可用)。您可以通过模式匹配来获取它:如果
fileChooserGetFilename
的结果返回Nothing
,这将失败并显示不透明的错误消息。为了提高鲁棒性,您可以对结果进行案例分析:另一个问题在这一行:
x <- m
用于将操作m
的结果绑定到变量中x
,但matchWord
返回一个Int
,而不是一个操作(例如,IO a
对于某些a )。
This will get you started.
At this point,
targetFile
has typeMaybe String
; that is, it will return eitherJust "somestring"
orNothing
. You want the"somestring"
part, if it's available. You can get it by pattern matching:This will fail with an opaque error message if the result of
fileChooserGetFilename
returnedNothing
. For more robustness you can case analyse the result:The other problem is in this line:
x <- m
is used to bind the result of an actionm
into the variablex
, butmatchWord
returns anInt
, not an action (eg.IO a
for somea
).