guard-clause

Pattern matching identical values

匆匆过客 提交于 2019-11-27 14:37:18
I just wondered whether it's possible to match against the same values for multiple times with the pattern matching facilities of functional programming languages (Haskell/F#/Caml). Just think of the following example: plus a a = 2 * a plus a b = a + b The first variant would be called when the function is invoked with two similar values (which would be stored in a ). A more useful application would be this (simplifying an AST). simplify (Add a a) = Mult 2 a But Haskell rejects these codes and warns me of conflicting definitions for a - I have to do explicit case/if-checks instead to find out

Pattern matching identical values

↘锁芯ラ 提交于 2019-11-26 16:49:30
问题 I just wondered whether it's possible to match against the same values for multiple times with the pattern matching facilities of functional programming languages (Haskell/F#/Caml). Just think of the following example: plus a a = 2 * a plus a b = a + b The first variant would be called when the function is invoked with two similar values (which would be stored in a ). A more useful application would be this (simplifying an AST). simplify (Add a a) = Mult 2 a But Haskell rejects these codes