Last active
August 29, 2015 13:56
-
-
Save patrickt/9025300 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- this would turn the expression | |
-- (((anArray[(10)]))) | |
-- into | |
-- anArray[10] | |
flatten :: Expr -> Expr | |
-- base case: do nothing to constants | |
flatten (Constant i) = Constant i | |
-- this is the important case: we shed the Paren constructor and just | |
-- apply `flatten` to its contents | |
flatten (Paren e) = flatten e | |
-- all the other cases preserve their constructors and just apply | |
-- the flatten function to their children that are of type `Expr`. | |
flatten (Index e i) = Index (flatten e) (flatten i) | |
flatten (Call e args) = Call (flatten e) (map flatten args) | |
flatten (Unary op arg) = Unary op (flatten arg) | |
flatten (Binary l op r) = Binary (flatten l) op (flatten r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment