I have this code that I want to make point-free;
(\k t -> chr $ a + flip mod 26 (ord k + ord t -2*a))
How do I do that?
Also are there some general rules for point free style other than “think about this amd come up with something”?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To turn a function
into point-free form, I generally try to follow what is done to the last parameter
zand write the function asThen I can cancel out the
zs to getThen repeating the process for y and x should end up with
funcin point-free form. An essential transformation to recognise in this process is:It’s also important to remember that with partial evaluation, you can “break off” the last argument to a function:
For your particular function, consider the flow that
kandtgo through:ordto each of themchrSo as a first attempt at simplifying, we get:
Note that you can avoid
flipby using a section onmod, and sections using-get messy in Haskell so there’s asubtractfunction (they clash with the syntax for writing negative numbers:(-2)means negative 2, and isn’t the same assubtract 2).In this function,
ord k + ord tis an excellent candidate for usingData.Function.on(link). This useful combinator lets us replaceord k + ord twith a function applied tokandt:We’re now very close to having
and hence
Unfortunately Haskell is a bit messy when it comes to composing a binary function with a sequence of unary functions, but there is a trick (I’ll see if I can find a good reference for it), and we end up with:
which is almost a nice neat point-free function pipeline, except for that ugly composing trick. By defining the
.:operator suggested in the comments on this page, this tidies up a little to:To polish this some more, you could add some helper functions to separate the letter <-> Int conversion from the Caesar cipher arithmetic. For example:
letterToInt = subtract a . ord