在Haskell中,如何在不创建单独函数的情况下使用'head'?

In Haskell, how do you use 'head' without creating a separate function?

我刚开始使用 Haskell,我不确定 'head' 是如何工作的。根据我的理解,它 returns 是列表中的第一个元素。我一直在尝试使用它,但我不断收到错误。我通过创建一个单独的函数来找到头部来添加一个变通方法,但是这似乎是不必要的。

我不明白为什么要在这里调用 findHead:

single x = length (snd(x)) == 1

toList board
    | board == [] = []
    | otherwise = filter single board

findHead board = head (toList board)

不等同于在这里调用toList:

single x = length (snd(x)) == 1

toList board
    | board == [] = []
    | otherwise =  head (filter single board)

我觉得这两个应该是一样的,但是只有第一个运行。为什么他们不被解释为相同的?你能给我解释一下吗?在上面的代码中,'board' 是一个元组列表,每个元组的形式都是 (x, [a,b,...])。

我在一些简单的事情中使用了 'head',例如:

union xs ys 
  | xs == [] = ys
  | ys == [] = xs
  | otherwise = union (tail xs)  (add (head xs) ys)

这似乎符合我的预期。

head 部分 。特别是,head [] 通常不会 return(抛出异常)。这在 Haskell 中可能很难处理,这就是为什么人们经常建议您避免部分函数。

那么我们该怎么做呢?我们必须在类型中反映失败

safeHead :: [a] -> Maybe a
safeHead []     = Nothing
safeHead (a:as) = Just a

可以为tail

做一个类似的函数
safeTail :: [a] -> Maybe [a]
safeTail []     = Nothing
safeTail (a:as) = Just as

我认为您需要退后一步,了解模式匹配、递归和单链表的工作原理。正如许多人所提到的,您正在编写的代码不会很好地工作。

作为示例,我将如何编写您问题中的各种函数:

single :: (a, [b]) -> Bool    
single (_, [_]) = True
single _ = False

toList :: [(a, [b])] -> [(a, [b])]
toList board = filter single board

findHead :: [(a, [b])] -> Maybe (a, [b])
findHead [] = Nothing
findHead board = head (toList board)

-- This one actually does the same thing as the built-in `++` operator,
-- but for the sake of illustration, I'll implement it.
union :: [a] -> [a] -> [a]
union [] ys = ys
union (x:xs) ys = x : union xs ys