constructor pattern matching haskell -


i have data type

data struct val =  empty | exec1 val                          | exec2 val  

and 2 dummy functions

apply :: struct -> string apply (empty) = "matched empty"   apply (exec struct) = "matched exec1 or exec2"                apply' :: struct val -> string apply' (empty)   = "matched empty"   apply' (exec1 _) = "matched exec1" apply' (exec2 _) = "matched exec2" 

second 1 working fine, first 1 causing error: "parse error in pattern: exec". can plase explain why can't match on constructor way: apply (exec struct) = ... ?

it's causing lot of boilerplate code when have multiple constructors in datatype , must pattern match them separately.

why? because can match constructors, , exec kind of new variable. 1 reason example following:

data struct2 =  empty | exec1 string                       | exec2 int  apply :: struct2 -> string apply empty = "matched empty"   apply (exec struct) = ?? 

how should know of exec1 , exec2 matching? couldn't apply functions here, since actual type of struct couldn't determined.

if want reduce pattern matching, there number of ways, using case, on different data implementation (like @karolis suggested) , helper functions higher level constructs more complex types. that's endless topic.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -