最好的写法是-> ..->[a] haskell 中的递归函数
所以我一直遇到这个小问题,
func :: a -> b -> [a] -- # or basically any a-> ...-> [a] where ... is any types ->
func x y = func' [x] y -- '# as long as they are used to generate a list of [a] from x
func' :: [a] -> b -> [a]
func' = undefined -- # situation dependant generates a list from
-- # each element and returns it as one long list
我应该保持这样吗?
我应该使用隐藏在何处的 func' 吗?
我应该只使用 [a] -> b-> [a] 版本并将传递[变量] 的责任留给被调用者?
我可能很需要组合这些函数,并且可能想打乱顺序,所以我倾向于选项 3。您觉得怎么样?
So I keep having this small problem where I have something like
func :: a -> b -> [a] -- # or basically any a-> ...-> [a] where ... is any types ->
func x y = func' [x] y -- '# as long as they are used to generate a list of [a] from x
func' :: [a] -> b -> [a]
func' = undefined -- # situation dependant generates a list from
-- # each element and returns it as one long list
should I keep it like this?
should I use func' hidden by a where?
should I only use the [a] -> b -> [a] version and leave the responsibility of passing [variable] to the callee?
I might well need to compose these functions and might want to mess around with the order so I'm leaning towards option 3. What do you think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在尝试重新发明 concatMap:
因此“map”位将输入列表的每个元素应用“f”。 “f”接受单个“a”并返回“[b]”。然后将这些单独的列表连接成一个列表。
It looks like you are trying to reinvent concatMap:
So the "map" bit takes each element of the input list applies "f" to it. "f" takes a single "a" and returns a "[b]". These individual lists are then concatenated into a single list.
正如 Paul 指出的,
func'
可以替换为concatMap
和func
。func
本身让我想起了unfoldr
from Data.List:用于从
生成
。a
列表b.顺便说一句,
func
和func'
对于此类函数来说是不幸的名称。As Paul noted,
func'
can be replaced withconcatMap
andfunc
.And
func
itself reminds me ofunfoldr
from Data.List:It is used to generate a list of
a
fromb
.By the way,
func
andfunc'
are unfortunate names for such functions.