高阶函数参数

黑色的

and_函数的问题

“错误:模式解析错误:f1”

 type Movie = (String, Double, String)

-- movies :: [(String, Double, String)]
movies :: [Movie]
movies = [ ("Green Book", 8.3, "Peter Farrelly")
     , ("Inception", 8.8, "Christopher Nolan")
     , ("Incredibles 2", 7.7, "Brad Bird")
     , ("The Dark Knight", 9.0, "Christopher Nolan")
     ]

imdbAtLeast :: Double -> Movie -> Bool
imdbAtLeast minRank (_, rank, _)
  | rank < minRank = False
  | otherwise      = True

director :: String -> Movie -> Bool
director expDir (_, _, dir)
  | expDir == dir = True
  | otherwise     = False

这是错误的功能

and_ :: (Movie -> Bool) -> (Movie -> Bool) -> Movie -> Bool
and_ (f1 param1) (f2 param2) (_, rank, dir)
  | f1 == director = (f1 param1 dir) && (f2 param2 rank)
  | otherwise      = (f1 param1 rank) && (f2 param2 dir)

这是对 and_ 函数的两个测试

and_ (director "Brad Bird") (imdbAtLeast 7.5) ("Incredibles 2", 7.7, "Brad 
Bird")
and_ (imdbAtLeast 7.5) (director "Brad Bird")  ("Incredibles 2", 7.7, "Brad 
Bird")
切普纳

您对通话and_没有看到director"Brad Bird"直接; 它获取应用结果的函数(对于第二个参数也类似。director"Brad Bird"

and_ :: (Movie -> Bool) -> (Movie -> Bool) -> Movie -> Bool
and_ f1 f2 (_, rank, dir) | ...

更大的问题是你不能比较函数的相等性。即使假设director本身作为一个独特的参数传递,你无法判断是否提到了同样的功能director

您想要的是简单地将前两个函数中的每一个应用于第三个函数并&&在结果上使用

and_ :: (Movie -> Bool) -> (Movie -> Bool) -> Movie -> Bool
and_ f1 f2 m = f1 m && f2 m

f1并且f2已经“知道”Movie他们正在检查论据的哪一部分

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章