data GroceryItem = CartItem ItemName Price Quantity | StockItem ItemName Price Quantity
makeGroceryItem :: String -> Float -> Int -> GroceryItem
makeGroceryItem name price quantity = CartItem name price quantity
I want to create a `GroceryItem` when using a `String` or `[String]`
createGroceryItem :: [String] -> GroceryItem
createGroceryItem (a:b:c) = makeGroceryItem a b c
输入的格式将会是 ["Apple","15.00","5"]
,我使用 Haskell 的 words
函数来拆分它。
我得到了以下错误,我认为这是因为 makeGroceryItem
接受一个 Float
和一个 Int
。
*Type error in application
*** Expression : makeGroceryItem a read b read c
*** Term : makeGroceryItem
*** Type : String -> Float -> Int -> GroceryItem
*** Does not match : a -> b -> c -> d -> e -> f*
但我如何使得b
和c
分别成为Float
和Int
类型?