gpt4 book ai didi

haskell - 无法用 where 和 pattern match 解释 Haskell 代码

转载 作者:行者123 更新时间:2023-12-02 05:40:25 24 4
gpt4 key购买 nike

我很难解析 mf m y 是如何赋值的,甚至为什么赋值的左侧可以有 3 个变量在 where 部分。

问:谁能解释一下这两种情况下发生了什么?(即空列表和包含一些元素的列表)

-- | A variant of 'foldl' that has no base case,
-- and thus may only be applied to non-empty structures.
--
-- @'foldl1' f = 'List.foldl1' f . 'toList'@
foldl1 :: (a -> a -> a) -> t a -> a
foldl1 f xs = fromMaybe (errorWithoutStackTrace "foldl1: empty structure")
(foldl mf Nothing xs)
where
mf m y = Just (case m of
Nothing -> y
Just x -> f x y)

(这是 source codefoldl1 function )。

最佳答案

where 子句中的定义遵循与全局定义相同的语法,因此 mf m y = ... 定义了一个名为 mf 的函数,它采用名为 my 的参数。

关于haskell - 无法用 where 和 pattern match 解释 Haskell 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46621625/

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