gpt4 book ai didi

haskell - LiftM (:[]) work

转载 作者:行者123 更新时间:2023-12-04 08:57:06 25 4
gpt4 key购买 nike

我试图理解下面的表达。它转换字符列表['a','b','c']到字符串列表 ["a", "b", "c"]

liftM (:[]) "abc"

这是怎么发生的?

最佳答案

机器猴头运算符(operator)(:[])只是 section缺点列表(:)和空列表[] ,即 (:[])相当于(\x -> x:[]) ;反过来也可以使用列表语法编写为 (\x -> [x]) .

这样重写,我们有

liftM (\x -> [x]) "abc"

字符串文字 "abc"也只是 character list ['a', 'b', 'c'] 的语法糖,所以我们可以反过来将上面的内容重写为
liftM (\x -> [x]) ['a', 'b', 'c']

liftM is just fmap from the Dark Days when Functor wasn't a superclass of Monad , 给
fmap (\x -> [x]) ['a', 'b', 'c']
Functor [] 的实例套 fmap = map , 给
map (\x -> [x]) ['a', 'b', 'c']

这减少到
[['a'], ['b'], ['c']]

或者,回到字符串表示法
["a", "b", "c"]

Q.e.d.

关于haskell - LiftM (:[]) work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31763252/

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