haskell - 用 zipWith 写 zip
问题描述
我正在通过 Chris Allen 和 Julie Moronuki 的 Haskell Programming 一书学习 Haskell。需要有关压缩列表练习之一的帮助。该练习有 3 个部分,前两个是关于编写我们自己的 zip 和 zipWith 实现,我能够做到。
myzip1 :: [a] -> [b] -> [(a,b)]
myzip1 _ [] = []
myzip1 [] _ = []
myzip1 (x:xs) (y:ys) = (x,y) : myzip1 xs ys
myZipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
myZipWith _ _ [] = []
myZipWith _ [] _ = []
myZipWith f (x:xs) (y:ys) = (f x y) : myZipWith f xs ys
最后一部分要求用 zipWith 编写 zip。我无法提出解决方案。老实说,我无法完全掌握这个问题(用第二个函数编写一个函数,当不需要第二个函数应用时,因为它首先需要所有参数)。谢谢您的帮助。
解决方案
myzip1
和之间的唯一区别myZipWith
是
myzip1 (x:xs) (y:ys) = (x,y) : myzip1 xs ys
myZipWith f (x:xs) (y:ys) = (f x y) : myZipWith f xs ys
也许对某些人myzip1
来说myZipWith f
是一样的f
。
myzip1 = myZipWith f
会f
怎样
(f x y) = (x, y)
推荐阅读
- python - 具有齐次坐标的 MVP 变换
- ios-app-group - 是否可以将数据从现有 iOS 应用程序发送到应用程序组的 Watch OS 6 独立应用程序成员?
- python - 将 Python 代码称为基于策略的设计的措施是什么?
- node.js - 将数据从服务器实时发送到前端的最佳方式(MEAN)
- asp-classic - MSXML2.ServerXMLHTTP 不在一台服务器上发送(返回?),但在所有其他服务器上发送
- r - 特定行列或表子集的 KabelExtra 背景颜色
- airflow - 为什么 Airflow 因 INFO 崩溃 - “任务退出并返回代码 -9”?
- flutter - StreamProvider 不更新状态
- web - 在哪里可以找到设备详细信息的像素比?
- java - byteBuffer.get() & 0xFF 的目的是什么?