- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究 Haskell,并成功地制定了一种算法来分解纸币中给定的货币值(value),并计算出该值(value)的总和。可以找到更好的解释(以及挑战本身)here .
import Text.Printf
import Data.List
import Data.Ord
filterNearest :: Int->(Int->Bool)
filterNearest a = (\x -> (max a x) <= a)
findNearest :: Int->[Int]->Int
findNearest x possibilities = last $filter (filterNearest x) possibilities
decomposeOnce :: Int->[Int]->[Int]->[Int]
decomposeOnce x list possibilities = [findNearest x possibilities] ++ list
decomposeRecursive :: Int->[Int]->[Int]->[Int]
decomposeRecursive x list possibilities = if x /= 0
then
let decomposed = decomposeOnce x list possibilities
in decomposeRecursive (x - decomposed!!0) decomposed possibilities
else list
countGroup :: [Int]->(Int, Int)
countGroup list = (list!!0, length list)
makeGroups :: [Int]->[(Int, Int)]
makeGroups list = map countGroup $group list
hasGap :: [(Int, Int)]->(Int->Bool)
hasGap dta = (\idx -> not $any (==idx) $map fst dta)
findGaps :: [(Int, Int)]->[Int]->[Int]
findGaps dta required = filter (hasGap dta) required
fillGaps :: [(Int, Int)]->[Int]->[(Int, Int)]
fillGaps dta gaps = dta ++ map (\x -> (x, 0)) gaps
sortData :: [(Int, Int)]->[(Int, Int)]
sortData dta = reverse $sortBy (comparing fst) dta
calc :: Int->[(Int, Int)]
calc x = let dta = makeGroups $decomposeRecursive x [] [1, 2, 5, 10, 20, 50, 100]
in sortData $fillGaps dta $findGaps dta [1, 2, 5, 10, 20, 50, 100]
formatData :: (Int, Int)->String
formatData dta = (show $snd dta) ++ " nota(s) de R$ " ++ (show $fst dta) ++ ",00\n"
main = do
x <- readLn
print x
printf $intercalate "" $map formatData $calc x
在某些情况下,我认为我可以使用函数组合运算符,但我无法正确应用它,所以我想寻求一些帮助来应用函数组合:
findNearest :: Int->[Int]->Int
findNearest x possibilities = last $filter (filterNearest x) possibilities
为什么我不能执行last.filter (filterNearest x)possibility
?
sortData :: [(Int, Int)]->[(Int, Int)]
sortData dta = reverse $sortBy (comparing fst) dta
为什么我不能reverse.sortBy Comparison.fst dta
?
我的概念有误吗?
最佳答案
函数组合.
与函数应用$
不同。您不能将 $
替换为 .
并期望程序具有相同的含义。它们是不同的东西。
一、函数应用。它的定义如下:
f $ x = f x
该运算符在左侧接受一个函数,在右侧接受一个值,并且仅将值作为参数传递给函数。以您的表达为例:
last $ filter (filterNearest x) possibilities
与运算符$
的定义进行比较:在您的代码中,f
是last
,x
是过滤(filterNearest x)可能性
。因此,您的代码相当于:
last (filter (filterNearest x) possibilities)
现在,让我们看看函数组合。它的定义如下:
f . g = \y -> f (g y)
这意味着组合两个函数 f
和 g
的结果是另一个函数,它将其参数传递给 g
然后将 g
的返回值传递给 f
。
现在看看您尝试的代码:
last . filter (filterNearest x) possibilities
与定义比较:f
是last
,g
是过滤器(filterNearest x)可能性
。这里需要注意的重要一点是,第二个参数filter (filterNearest x)possibility
不是一个函数!所以难怪它不能通过函数组合来组合。
但事实上,你的直觉是正确的(或者这是你的家庭作业?):函数组合确实可以在这种情况下使用,并提供一些好处。
让我们看一下这个表达式:
last . filter (filterNearest x)
与定义比较:f
是last
,g
是filter (filterNearest x)
。现在,两个参数实际上都是函数,因此可以应用函数组合。要查看应用它的结果,只需使用替换:
last . filter (filterNearest x)
== \y -> last (filter (filterNearest x) y)
因此,这种组合的结果是一个函数,该函数采用列表作为参数,过滤该列表,然后获取结果的最后一个元素。因此 findNearest
的完整定义可能如下所示:
findNearest :: Int -> [Int] -> Int
findNearest x = last . filter (filterNearest x)
看看函数组合拯救了你什么?现在您不必写出第二个参数!大多数 Haskell 程序员会认为这是一个好处,但我也知道有些人对此会皱眉,认为这会使程序更难以理解。每个人都有自己的看法,但注意到这种分歧很有用。
我会将 sortData
的类似转换作为练习。
关于haskell - Haskell 中的钞票找零机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381470/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!