Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currying 3 Arguments in Haskell

I'm having trouble with currying a function to remove three arguments in Haskell.

Disclaimer: Not Coursework, I was asked this question by someone struggling with this today and it's been bugging me.

The custom types/functions we were given were (can only remember types)

type MyThing
  = (Char, String)
type MyThings
  = [MyThing]

funcA :: MyThings -> String -> String
funcB :: MyThings -> String -> Int -> String

We started with:

funcB as str n = iterate (funcA as) str !! n

And reduced it down as follows:

funcB as str n = iterate (funcA as) str !! n
funcB as str = (!!) . (iterate (funcA as)) str
funcB as = (!!) . (iterate (funcA as))
funcB as = (!!) . (iterate . funcA) as

Then, stuck. We just can't figure out how to avoid using the last argument. I know I've seen a similar situation somewhere before and there was a solution.

Hoping some Haskell genius can point out why I'm being an idiot...

like image 906
Pete Hamilton Avatar asked Oct 30 '12 13:10

Pete Hamilton


People also ask

What does currying mean Haskell?

From HaskellWiki. Currying is the process of transforming a function that takes multiple arguments in a tuple as its argument, into a function that takes just a single argument and returns another function which accepts further arguments, one by one, that the original function would receive in the rest of that tuple.

What is the difference between currying and partial application?

Currying: A function returning another function that might return another function, but every returned function must take only one parameter at a time. Partial application: A function returning another function that might return another function, but each returned function can take several parameters.

Is currying a higher-order function?

Higher-order functions are functions that take functions as parameters or return functions when called. Curried functions are higher-order functions that take one argument at a time returning a series of functions until all parameters are passed.


1 Answers

All you need here is the following three "laws" of operator sections:

(a `op` b) = (a `op`) b = (`op` b) a = op a b
          (1)          (2)          (3)

so that the operand goes into the free slot near the operator.

For (.) this means that: (a . b) = (a .) b = (. b) a = (.) a b. So,

f (g x) y !! n      
= (!!) (f (g x) y) n              by (3) 
= ((!!) . f (g x)) y n
= ((!!) . (f . g) x) y n
= ((!!) .) ((f . g) x) y n        by (1)
= (((!!) .) . (f . g)) x y n
= (((!!) .) . f . g) x y n

You should only do as much pointfree transformation as you're comfortable with, so that the resulting expression is still readable for you - and in fact, clearer than the original. The "pointfree" tool can at times produce unreadable results.

It is perfectly OK to stop in the middle. If it's too hard for you to complete it manually, probably it will be hard for you to read it, too.

((a .) . b) x y = (a .) (b x) y = (a . b x) y = a (b x y) is a common pattern that you will quickly learn to recognize immediately. So the above expression can be read back fairly easily as

(!!) ((f . g) x y) n = f (g x) y !! n

considering that (.) is associative:

(a . b . c) = ((a . b) . c) = (a . (b . c))
like image 153
Will Ness Avatar answered Sep 21 '22 06:09

Will Ness