Aşağıdaki kod bir sözcük kitaplığı programıdır. Bir önek, sonek ve bazı harflerle eşleşen sözcüklerin bir listesini aramanızı sağlar. Sorum şu: altta listeyi kullanmak yerine, kelimeleri içeren harici bir metin dosyası kullanmak ve listeye yüklemek istiyorum. Bunu yapmayı nasıl yaparım?Haskell harici txt dosyasını listeye yükle
count :: String -> String -> Int
count _[] = 0
count [] _ = 0
count (x:xs) square
|x `elem` square = 1 + count xs (delete x square)
|otherwise = count xs square
check :: String -> String -> String -> String -> Bool
check prefix suffix word square
| (length strippedWord) == (count strippedWord square) = True
| otherwise = False
where
strippedWord = drop (length prefix) (take ((length word) - (length suffix)) word)
wordfeud :: String -> String -> String -> [String]
wordfeud a b c = test1
where
test =["horse","chair","chairman","bag","house","mouse","dirt","sport"]
test1 = [x| x <- test, a `isPrefixOf` x, b `isSuffixOf` x, check a b x c]
Kodunuzla ilgili iki kısa ipucu: 1. Haskell işlev uygulamasında infix operatörlerinden daha güçlü bir öncelik vardır. Yani, 'length strippedWord == count strippedWord square' gibi bir ifade '(length strippedWord) == (strippedWord square)' e eşdeğerdir. 2. 'check' fonksiyonunda fonksiyonun sonucu eşitlik kontrolünün değeridir. Bu nedenle, korumayı (dikey çubukları olan kısmı) eşitlik kontrolüyle değiştirebilirsiniz. –
Bu satırı sadece bu satır ile değiştirebilirim "length strippedWord == count strippedWord square = True"? – tutu
Kusura bakmadığı için özür dilerim: 'önek sonekini onayla 'kare = uzunluk strippedWord == count strippedWord square' komutunu kullanabilirsiniz. Yani, 'check' uygulamasının sonucu '==' uygulamasının sonucudur. –