Provide a monad that provides every possible parenthesization of the words of
its argument. In addition to the standard brevity/elegance/speed metrics, bonus
points will be awarded for elision of superfluous parens, ordering results by
number of parens, and tacit solutions.
Here's an example to get you started:
parenthesize =: verb define
if. 1 = # y do.
,<'(',')',~;y return.
end.
m =. (3 #:@:+ [: i.@:<:&.-: 2^#) y
M =. m <@:(<;.2)"1 y
; ('(',')',~;)each each ,@:{ each parenthesize each each M
)
> parenthesize ;:'a b c d'
((((a)(b))(c))(d))
(((a)((b)(c)))(d))
(((a)(b)(c))(d))
(((a)(b))((c)(d)))
(((a)(b))(c)(d))
((a)(((b)(c))(d)))
((a)((b)((c)(d))))
((a)((b)(c)(d)))
((a)((b)(c))(d))
((a)(b)((c)(d)))
((a)(b)(c)(d))
-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm