我在编写教授给我的这段代码时遇到了问题:
Write a function called
digit7
which takes anInt
and returns aBool
saying whether or not 7 is one of the digits. (Hint: useshow
to turn the number into a list of characters.) Usedigit7
to create a function of no parameters calledsquare7
which returns the smallest number whose square contains a 7 as a digit.
我的代码是:
digit7 l = elem '7' (show l)
这行得通,但是,我需要以无点样式编写的代码。我也无法理解 square7
函数。
最佳答案
对于 digit7
函数,您可以使用函数组合将您的定义转换为无点样式:
digit7 = (elem '7') . (show)
这是因为:
digit7 l
-> ((elem '7') . (show)) l By substitution
-> (elem '7') ((show) l) By definition of (.)
-> elem '7' (show l) By operator precedence
关于square7
函数,我推荐使用dropWhile
和head
。
关于haskell - 将函数定义更改为无点样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33379473/