[Haskell-cafe] pattern matching vs if-then-else
Maarten Faddegon
haskell-cafe at maartenfaddegon.nl
Sun Aug 12 13:30:54 CEST 2012
Hi there,
I am writing a toy compiler in Haskell to further my skills in
functional programming. One of the functions I wrote is to determine the
iteration count of a loop. I have a number of different test that I want
to do and I find myself now testing some of these using pattern matching
and some properties using an if-then-else construction. I do not
consider this very pretty.
My question is: are there guidelines of when to use pattern matching and
when to use if-then-else?
Snippet of the function I mentioned:
---8<-----------------------------------------------------------------------------------
itercount (ForLoop
[ ( Assignment update_lcv
(Op2 "+" (Content update_lcv') update_expr)
)
]
[(Assignment init_lcv init_expr)]
(TestStmt (Op2 "<" (Content test_lcv) test_expr))
bodyblock)
= if -- All stmts use the same lcv
test_lcv == init_lcv
&& test_lcv == update_lcv
&& test_lcv == update_lcv'
-- And the lcv is not updated in the body
&& intersect [test_lcv] (blockkills bodyblock) == []
then Just $ simple_itercount init_expr test_expr
update_expr
else Nothing
itercount _ = Nothing
---8<-----------------------------------------------------------------------------------
Thanks,
Maarten
More information about the Haskell-Cafe
mailing list