[英]Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])'
[英]Haskell Error - Couldn't match type `[Int] -> String' with `[Char]'; Expected type: String; Actual type: [Int] -> String
我正在嘗試在 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"
問題出在這一行
histogram xs = histogram' . toCounts
它應該是
histogram xs = histogram' $ toCounts xs
或者
histogram = histogram' . toCounts
你寫的是函數組合,它產生[Int] -> String
但是你已經聲明了另一個變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.