Haskell:使用部分应用程序将列表 xs 与列表 yss 中的每个列表一起压缩

发布于 2024-08-17 09:32:28 字数 405 浏览 5 评论 0原文

嘿,我正在复习周五即将举行的函数式编程考试,我正在做我们讲师给出的练习。我遇到了一个需要帮助的问题:

6。 a) 编写一个函数,将给定列表 xs 与列表列表 yss 中的每个列表进行压缩。使用部分 在定义函数时尽可能使用应用程序和 lambda 表达式。 b) 编写一个函数,用给定的列表 xs 压缩列表 yss 中的每个列表。使用部分 在定义函数时尽可能使用应用程序和 lambda 表达式。 该问题的 a) 部分和 b) 部分的解决方案之间的差异部分说明了 应用函数必须以正确的顺序获取参数。

目前我对 (a) 的了解是:

zipAll = (\xs (ys:yss) -> [(zip xs ys)] ++ zipAll xs yss)

我知道这并不详尽,但有人能给我一些指示吗?

Hey I'm just revising for my functional programming exam coming up on friday and I'm just working through exercises given by our lecturer. I've come across one which I neep a bit of help on:

6. a) Write a function that zips a given list xs with every list in a list yss of lists. Use partial
applications and lambda expressions to the greatest extent possible in defining your function.
b) Write a function that zips every list in a list yss of lists with a given list xs. Use partial
applications and lambda expressions to the greatest extent possible in defining your function.
The difference between your solutions to part a) and part b) of this problem illustrates that partially
applied functions must take their arguments in the correct order.

What I have at the moment for (a) is :

zipAll = (\xs (ys:yss) -> [(zip xs ys)] ++ zipAll xs yss)

It's non-exaustive I know but could anyone give me some pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-08-24 09:32:28

看来您只是缺少使该功能正常工作的基本情况。基本情况是

(\xs [ys] -> [(zip xs ys)])

我可能会将参数移到方程的右侧:

zipAll xs (ys:yss) = ...

但这只是风格问题。

(b) 部分的解决方案是相同的,只是参数的顺序相反,例如

zipAll (ys:yss) xs = ...

It seems like you are just missing your base case to make the function work. The base case would be

(\xs [ys] -> [(zip xs ys)])

I would probably move the parameters to the right side of the equation instead:

zipAll xs (ys:yss) = ...

but that is just at matter of style.

The solution for part (b) is the same, except that the order of the parameters is reversed, like

zipAll (ys:yss) xs = ...
一袭水袖舞倾城 2024-08-24 09:32:28

没关系,我解决了:

zipList :: [a] -> [[b]] -> [[(a,b)]]
zipList = \xs yss -> c xs yss
                        where
                                c xs [] = []
                                c xs (ys:yss) = zip xs ys :zipList xs yss

只是希望我现在没有发布这个:P

Its fine, I worked it out:

zipList :: [a] -> [[b]] -> [[(a,b)]]
zipList = \xs yss -> c xs yss
                        where
                                c xs [] = []
                                c xs (ys:yss) = zip xs ys :zipList xs yss

Just wish I hadn't posted this now :P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文