[Haskell-cafe] A useful trick for inclusion and exclusion of commented code

Sunil KOTHARI sunilkot at comp.nus.edu.sg
Mon Apr 12 06:14:37 EDT 2004

Here's a little trick that I often use for inclusion and exclusion of
a chunk of code.
Imagine we have  a  function foo defined below.
The three lines of code  can be included
or excluded  just by a changing a single character --- that's the trick.
So, the idea is to comment our code in a slightly different way like
this to exclude the code from executing:
1 {-
2 myand :: Bool -> Bool -> Bool
3 myand a b = if a == b then True
4                      else False
5 --}

Notice how line  5 is commented '--}' instead of '-}'. Now, if we change
the first character in line 11 from '{' to '-' the entire chunk of code
from line 12-14 no longer remains comment.

11 --
12 myand :: Bool -> Bool -> Bool
13 myand a b = if a == b then True
14                      else False
15 --}

This trick can also be applied to C++ comments.

Best wishes,

More information about the Haskell-Cafe mailing list