こんにちは私はいくつかの単語検索プログラムを作っています
例えば
「text.txt」ファイルに「foofoosfoorfo..foofool」が含まれている場合
「foo」を検索します
次に2番だけが印刷されます
何度も何度も検索します
でも私はhaskellの初心者です
私のコードはここにあります
: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
}
非常に貧弱なコードでごめんなさい
私の開発環境はWindowsXPSP3WinGhci1.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)