gpt4 book ai didi

haskell - 第二个最近的列表项 Haskell

转载 作者:行者123 更新时间:2023-12-04 05:16:30 24 4
gpt4 key购买 nike

我正在尝试获取 Haskell 列表中的第二项。我认为最好的方法是获取列表尾部的头部。

secondMostRecentChoice :: History -> Choice // a choice is just a Bool like Bake | NoBake
secondMostRecentChoice [] = "Not Enough History"
secondMostRecentChoice history =
if ( length history == 1 )
then "Still Not Enough History"
else if (length history >= 2)
then (head [b | (head a,b) <- history]) //Here is the problem
else "Not supposed to be here"
else "Not supposed to be here"

但我得到以下信息:

Parse error in pattern: head
Possibly caused by a missing 'do'?



为什么我需要做 do或者这是一个错误的建议?

最佳答案

做到这一点的最短方法就是模式匹配它。

secondElem :: [a] -> Maybe a
secondElem (_:x:_) = Just x
secondElem _ = Nothing

关于haskell - 第二个最近的列表项 Haskell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25870927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com