你好,我是做一些單詞搜索程序哈斯克爾單詞搜索程序開發
例如
當「的text.txt」文件包含「富FOOS福爾購買..富傻瓜」和搜索
「foo」 的
然後只數2印刷
並再次並重新搜索
但我哈斯克爾初學者
我的代碼是在這裏
:module +Text.Regex.Posix
putStrLn "type text file"
filepath <- getLine
data <- readFile filepath
--1. this makes <interactive>:1:1: parse error on input `data' how to fix it?
parsedData =~ "[^- \".,\n]+" :: [[String]]
--2. I want to make function and call it again and again
searchingFunc = do putStrLn "search for ..."
search <- getLine
result <- map (\each -> if each == search then count = count + 1) data
putStrLn result
searchingFunc
}
對不起,非常非常差的代碼
我的開發環境是Windows XP SP3 WinGhci 1.0.2
我開始幾小時前哈斯克爾對不起
非常感謝您的閱讀!
編輯:這裏是原來的計劃代碼
謝謝!
#lang scheme/gui
(define count 0)
(define (search str)
(set! count 0)
(map (λ (each) (when (equal? str each) (set! count (+ count 1)))) data)
(send msg set-label (format "~a Found" count)))
(define path (get-file))
(define port (open-input-file path))
(define data '())
(define (loop [line (read-line port)])
(when (not (eof-object? line))
(set! data (append data
(regexp-match* #rx"[^- \".,\n]+" line)))
(loop)))
(loop)
(define (cb-txt t e) (search (send t get-value)))
(define f (new frame% (label "text search") (min-width 300)))
(define txt (new text-field% (label "type here to search") (parent f) (callback (λ (t e) (cb-txt t e)))))
(define msg (new message% (label "0Found ") (parent f)))
(send f show #t)
我明白了,您在REPL中輸入了該代碼。 Haskell不是腳本語言(儘管可以用它來編寫腳本),通常編寫代碼的方法是將其放入一個後綴爲「.hs」的文件中。 – fuz 2011-05-31 14:00:34
非常感謝您的建議。還要別的嗎? – 2011-05-31 14:03:06
不要使用'data'作爲標識符。它是Haskell中引入新數據類型的保留字。 – fuz 2011-05-31 14:04:21