我正在嘗試在 Haskell 中運行 Hopscotch 練習,但在編譯時出現此錯誤。將不勝感激任何幫助。
我檢查了引數和輸出,它們似乎是正確的。
這是錯誤資訊:
Histogram1.hs:11:16: error:
* Couldn't match type `[Int] -> String' with `[Char]'
Expected type: String
Actual type: [Int] -> String
* Probable cause: `(.)' is applied to too few arguments
這是哈斯克爾代碼:
module Histogram where
import qualified Data.Map as M
--- Type Aliases
type Count = Int
type Counts = M.Map Int Count
type Indices = [Int]
histogram :: [Int] -> String
histogram xs = histogram' . toCounts
where
histogram' :: Counts -> String
histogram' xs =
let (maxs, xs') = trim xs
in if null maxs
then footer
else asterisks maxs histogram' xs'
footer :: String
footer = unlines ["=========="
, "0123456789"
]
toCounts :: [Int] -> Counts
toCounts = foldr insertOrAdjust M.empty
asterisks :: [Int] -> String
asterisks ns = map (\n -> if n `elem` ns then '*' else ' ') [0..9] "\n"
uj5u.com熱心網友回復:
問題出在這一行
histogram xs = histogram' . toCounts
它應該是
histogram xs = histogram' $ toCounts xs
或者
histogram = histogram' . toCounts
你寫的是函陣列合,它產生了[Int] -> String但是你已經宣告了另一個變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/329028.html
標籤:哈斯克尔
上一篇:Haskell:在地圖上遍歷
