Being a Haskell hacker, I prefer pointfree notation over pointful. Unfortunately some people find pointfree notation hard to read, and I find it hard to get the correct number of parentheses when I write in pointful. Help me convert code written in pointfree to pointful notation!
About
In pointfree notation we use points (yes, really) to feed the output of one function into another. Say, if you had a function succ
that takes a number and adds 1 to it, and you wanted to make a function that adds 3 to a number, instead of doing this:
\x -> succ(succ(succ(x)))
you could do this:
succ.succ.succ
Pointfree only works with functions that take a single parameter however (in this challenge anyway), so if our function wasn't succ
but rather add
which take 2 numbers and adds them together, we would have to feed it arguments until there is only one left:
pointful: \x -> add 1(add 1(add 1 x))
pointfree: add 1 . add 1 . add 1
Lastly, functions may take other functions as arguments:
Pointfree: map (f a . f b) . id
Pointful: \x -> map (\x -> f a (f b x)) (id x)
Javascript equivalent: x => map (x => f(a,f(b,x)), id(x))
Input and expected output
f . f . f
\x -> f (f (f x))
f a . f b . f c
\x -> f a (f b (f c x))
f (f a . f b) . f c
\x -> f (\x -> f a (f b x)) (f c x)
a b c . d e . f g h i . j k l . m
\x -> a b c (d e (f g h i (j k l (m x))))
a.b(c.d)e.f g(h)(i j.k).l(m(n.o).p)
\x->a(b(\y->c(d y))e(f g h(\z->i j(k z))(l(\q->m(\w->n(o w))(p q))x)))
Rules
- Your output may have more spaces or parentheses than required, as long as they are balanced
- You don't have to make sure that the name of the variable you create
\x
isn't already used somewhere else in the code - It is your choice whether to create a function or a full program
- This is
codegolf
, shortest code in bytes win!
You might find blunt useful, it converts between the two notations (but also factorizes the code when possible): https://blunt.herokuapp.com
(+).(*3)
is the same as\x y->3*x+y
\$\endgroup\$(.).(.)
which converts to\i b c f -> i (b c f)
\$\endgroup\$.
with a(
, prepend a\x
and append a correspondingx
and as many)
as are required? Or is it more complicated than that? \$\endgroup\$\ d->f(\k->f(f d k))
, but you can assume that all the dots are fed two arguments in this challenge \$\endgroup\$