Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8056607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:45:45+00:00 2026-06-05T08:45:45+00:00

I have this data type data Struct val = Empty | Exec1 val |

  • 0

I have this data type

data Struct val =  Empty | Exec1 val
                         | Exec2 val 

And two 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 one is working fine, but the first one is causing error: “Parse error in pattern: exec”. Can you plase explain why can’t I match on constructor this way:
apply (exec struct) = … ?

It’s causing a lot of boilerplate code when I have multiple constructors in my datatype and must pattern match them all separately.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:45:47+00:00Added an answer on June 5, 2026 at 8:45 am

    Why? Because you can only match constructors, and exec is kind of a new variable. One reason for this is for example the following:

    data Struct2 =  Empty | Exec1 String
                          | Exec2 Int
    
    apply :: Struct2 -> String
    apply Empty = "matched Empty"  
    apply (exec struct) = ??
    

    How should anyone know which of Exec1 and Exec2 you are matching? You couldn’t apply functions here, since the actual type of struct couldn’t be determined.

    If you want to reduce the pattern matching, there are a number of ways, from using case, over different data implementation (like @Karolis suggested) and helper functions to higher level constructs with more complex types. But that’s an endless topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
Say i have a data structure like this: type Foo struct { Bar []struct
I have this type of data: entity, account, month1, month2, ..., month12 abc, 2000.02,
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
I have grabbed some XML data using this piece of jQuery: $.ajax({ type: POST,
I have a table with data like this. Id PId Device Status Type Created_Date
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
I have a set data type: template <class V> struct Set { void add(const
I have some example data: public struct Task { public int INT; public string
i need to create a data type (struct in this case) with an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.