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

标签 haskell functional-programming pointfree

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

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/

相关文章:

haskell - 类型的总函数 (forall n . Maybe (f n)) -> Maybe (forall n . (f n))

sql - 通过键列表获取多个持久条目

haskell - 有没有希望将 ForeignPtr 转换为 ByteArray#(对于函数::ByteString -> Vector)

javascript - 编写具有多个参数的 javascript 函数

javascript - lambda : rewriting to point free style

haskell - 可变参数组合函数?

haskell - 尝试在Haskell中使用存在类型时出现编译错误

scala - 使用 Scala 在类型级编程中推广 DivisibleBy

functional-programming - 榆树 : extract a record values from a list of string

Haskell 柯里化(Currying)以删除最后的参数变量