简体   繁体   English

Haskell无法与列表匹配

[英]Haskell won't match against a list

I want to try out some haskell and started with 99 haskell problems, currently I'm stuck at 13: Run-length encoding of a list (though my question is more general) 我想尝试一些haskell并从99个haskell问题开始,目前我停留在13:列表的游程长度编码(尽管我的问题更笼统)

--datatype
data En a = Multiple (Int, a) | Single a | Empty
    deriving (Show)

--helper
getnum :: (Eq a9) => [a9] -> Int
getnum [] = 0
getnum (x:xs)
    | x == head xs = 1 + getnum xs
    | otherwise = 1

encodeDirect :: [a] -> [En a]
endoceDirect xxs@(x:xs)
    | getnum xxs == 1 = Single x : encodeDirect xs
    | otherwise = Multiple ((getnum xxs), x) : encodeDirect (drop (getnum xxs) xxs)
encodeDirect _ = [Empty]

This should give me the solution with an [Empty] at the end, but when I call encodeDirect with any kind of list/string from ghci it just falls straight through, and all I get is [Empty]. 这应该给我最后一个[Empty]的解决方案,但是当我用ghci中的任何类型的列表/字符串调用encodeDirect时,它就直接掉了,而我得到的只是[Empty]。

Why does xxs@(x:xs) not match any list? 为什么xxs@(x:xs)不匹配任何列表?

It should give you at least a warning about function endoceDirect , which, of course, doesn't have anything to do with encodeDirect . 它应该至少给您关于函数endoceDirect的警告,当然,该函数与encodeDirect没有任何encodeDirect

As a side note, the catch-all case is usually a bad idea, 顺带一提,全面解决通常不是一个好主意,

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

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