gpt4 book ai didi

haskell - 将函数定义更改为无点样式

转载 作者:行者123 更新时间:2023-12-01 11:29:51 25 4
gpt4 key购买 nike

我在编写教授给我的这段代码时遇到了问题:

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函数,我推荐使用dropWhilehead

关于haskell - 将函数定义更改为无点样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33379473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com