Search code examples
haskellfunctional-programmingpointfree

Changing function definition to point-free style


I'm having trouble writing this code that my professor has given me:

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.

The code that I have is:

digit7 l = elem '7' (show l)

This works but, I need the code written in a point-free style. I am also having trouble figuring out the square7 function as well.


Solution

  • For the digit7 function, you can convert your definition to point-free style by using functional composition:

    digit7 = (elem '7') . (show)
    

    This is because:

       digit7 l
    -> ((elem '7') . (show)) l     By substitution
    -> (elem '7') ((show) l)       By definition of (.)
    -> elem '7' (show l)           By operator precedence
    

    As for the square7 function, I recommend using dropWhile and head.