简体   繁体   English

应用程序中输入错误-Haskell?

[英]Type error in application - Haskell?

type Car = (String, [String], Int, [String]) 类型Car =(字符串,[字符串],整数,[字符串])

carToString :: [Car] -> IO()
carToString [] = putStr ""
carToString (x:xs) = putStr x ++ "\n" : putStr xs ++ "\n"

displayAllCars :: IO ()
displayAllCars = putStr carToString testDatabase --< test data in the format of type Car

this gives me the error: 这给了我错误:

ERROR file:.\template.hs:26 - Type error in application
*** Expression     : putStr xs
*** Term           : xs
*** Type           : [([Char],[[Char]],Int,[[Char]])]
*** Does not match : [Char]

What is the cause of this error and how do I correct it? 此错误的原因是什么,我该如何纠正?

Try instead 试试吧

displayAllCars :: [Car] -> IO ()
displayAllCars = mapM_ (putStrLn . show)

naturally this generalizes to something like 自然,这可以概括为

putStrLnAll :: Show a => [a] -> IO ()
putStrLnAll = mapM_ (putStrLn . show)

I think this does what you desire. 我认为这可以满足您的需求。 Your code is pretty much indecipherable because the names of the functions don't match what they actually do. 您的代码几乎难以理解,因为函数的名称与它们的实际名称不匹配。

An example of iterating over the Cars: 遍历汽车的示例:

iter [] = ?
iter x@(v1, v2, v3, v4):xs = do stuff with x (the car) and its values v1, v2, v3, v4 then call iter xs.

To fold them all into a string, you probably want something like (assuming displayCar :: Car -> String exists). 要将它们全部折叠成一个字符串,您可能想要类似(假设displayCar :: Car -> String存在)。 I'm again avoiding explicit recursion here in favor of using an auxiliary function. 我再次在这里避免显式递归,而倾向于使用辅助函数。

displayAllCars = foldl' (\acc val -> acc ++ "\n" ++ val) []

However, we could use recursion: (The usual warning applies here as with any non-tail-optimized recursion function. You will get a stack overflow if the list is large. Use the foldl' version in production code. Alternatively foldr is the best if a backwards list is acceptable. 但是,我们可以使用递归:(通常的警告适用于任何非尾部优化的递归函数。如果列表很大, 则会出现堆栈溢出。在生产代码中使用foldl'版本。或者,foldr是最好的如果可以接受向后列表。

displayAllCars [] = []
displayAllCars c:cs = displayCar c ++ "\n" ++ displayCar cs

When you write: 当你写:

putStr x ++ "\n"

this will be parsed like so: 它将像这样解析:

(putStr x) ++ "\n"

This is probably not what you want. 这可能不是您想要的。 Try 尝试

putStr ( x ++ "\n" )

Note also that the : operator has type a -> [a] -> [a], ie the second operand must be a list. 还要注意:运算符的类型为-> [a]-> [a],即第二个操作数必须为列表。 But you have the same kind of thing left and right of : This would explain the exotic error message, as it induces the compiler to match an already wrong type with its list form ..... 但是,您在左右有同样的事情:这将解释异常的错误消息,因为它会导致编译器将已经不正确的类型与其列表形式匹配..

Among other things, the one problem that doesn't seem to have been pointed out yet is that you're trying to apply putStr (which expects a [Char] ) to carToString (a function which, even after being given parameters, returns an IO () ). 除其他事项外,似乎尚未指出的一个问题是,您试图将putStr (期望[Char] )应用于carToString (该函数即使在被赋予参数后carToString返回IO () )。 As carToString already makes use of putStr , just get rid of the putStr in displayAllCars . 由于carToString已经使用了putStr ,因此只需在displayAllCars删除putStr

Try this : 尝试这个 :

carToString :: [Car] -> [IO()]
carToString [] = [putStr ""]
carToString (x:xs) = putStr x ++ "\n" : carToString xs ++ "\n"

You don' need display all cars. 您不需要显示所有汽车。 You can just call the carToString function and pass the list of Car 您可以只调用carToString函数并传递Car列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM