Tim Newsham wrote:
I was rewriting some non-haskell code in haskell and came up with this
construct:
foreach l f = mapM_ f l
main = do
args <- getArgs
foreach args (\arg -> do
foreach [1..3] (\n -> do
putStrLn ((show n) ++ ") " ++ arg)
)
)
which is reminiscent of foreach in other languages. Seems fairly
useful and I was wondering how hard it would be to add some syntactic
sugar to the "do" construct to make it a little prettier (ie.
not require the parenthesis, binding and nested do, as:
main = do
args <- getArgs
foreach args arg
foreach [1..3] n
putStrLn ((show n) ++ ") " ++ arg)
would this type of transformation be possible with template haskell
or does this need stronger support from the parser to pull off?
I'm pretty sure you need parser support to pull off something like this,
if by "pull off" you mean providing this syntax with less lexical
overhead than the pure Haskell code. You'll have $( ) from a macro
invocation, and [| |] around the body, or putting the body in a string
literal.
TH is handy for metaprogramming, but not very good for syntax extension.
As for syntax design, the original isn't so bad. The only thing truly
useless are the parentheses or $. Some visual indication that args is
being bound is nice, plus the \bindings notation scales nicely to
constructs binding more names. "do" is arguable, at least it seems
pretty popular to use something similar with loops in syntaxes heavier
on keywords than symbols.
Couldn't '\' delimit a subexpression, as parentheses do? Would there be
any ambiguity in accepting code like State \s -> (s, s) instead of
requiring State $ \s -> (s, s), or taking
main = do
args <- getArgs
foreach args \arg -> do
foreach [1..3] \n -> do
putStrLn ((show n) ++ ") " ++ arg
It would be a bit odd to have a kind of grouping the always starts
explicitly and ends implicitly, but other than that it seems pretty
handy, harmless, and natural (I know I've tried to write this sort of
thing often enough)
Brandon
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe