将函数定义更改为无点样式
Changing function definition to point-free style
我在编写教授给我的这段代码时遇到了问题:
Write a function called digit7
which takes an Int
and returns a Bool
saying whether or not 7 is one of the digits. (Hint: use show
to
turn the number into a list of characters.) Use digit7
to create a
function of no parameters called square7
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
。
我在编写教授给我的这段代码时遇到了问题:
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
。