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 8437193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:21:58+00:00 2026-06-10T07:21:58+00:00

I am learning Haskell with the help of Learn You a Haskell for Great

  • 0

I am learning Haskell with the help of “Learn You a Haskell for Great Good!” and am currently trying to understand typeclasses and instances.
LYAH provides an example where a type called TrafficLight is defined as follows:

data TrafficLight = Red | Yellow | Green

Now TrafficLight is supposed to be an instance of Eq displaying the following behaviour:

instance Eq TrafficLight where
    Red == Red = True
    Green == Green = True
    Yellow == Yellow = True
    _ == _ = False

In order to understand how this works, I wrote my own file called Shop.hs where I try to override the behaviour of Eq for my ItemSlot.

module Shop where

type Number = Int

data Item =
          BellPepper
        | Cabbage
        | Carrot
        | Lettuce
        | Onion
        | Potato
        | Tomato
        deriving (Show, Read, Eq)

data ItemSlot = ItemSlot {
        item :: Item,
        number :: Number
        } deriving (Show)

instance Eq ItemSlot where
        ((item a) == (item a)) = True -- line that contains the error
        _ == _ = False

However, if I load the file in GHCi, I get the following error:

Prelude> :l Shop.hs 
[1 of 1] Compiling Shop             ( Shop.hs, interpreted )

Shop.hs:21:11: Parse error in pattern: item
Failed, modules loaded: none.

(I must admit that I am rather confused as to what the correct syntax is here – is it item a or just item?
Using only item fails with the same error, and using more parentheses – as was the answer in another question like this on SO – does not seem to help either.)

My guess is that I cannot use the item function provided by the record-syntax that is used in ItemSlot, but nevertheless I do not know how to resolve the issue.

  • 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-10T07:21:59+00:00Added an answer on June 10, 2026 at 7:21 am

    Patterns typically begin with a constructor. The constructor for the ItemSlot type is ItemSlot, so you would use that:

    instance Eq ItemSlot where
        ItemSlot item a == ItemSlot item' a' = -- use item, item', a, and a'
    

    Alternately, because you’ve defined ItemSlot as a record, there’s so-called record syntax for patterns. You can bind variables by name rather than position:

    instance Eq ItemSlot where
        ItemSlot { item = foo, number = a } == ItemSlot { item = foo', number = a' }
            = -- use foo, foo', a, and a'
    

    You can of course shadow names, if you don’t mind a chance of confusion:

    instance Eq ItemSlot where
        ItemSlot { item = item, number = a } == ItemSlot { item = item', number = a' }
            = -- use item, item', a, and a'
    

    For convenience, patterns in Haskell can be nested; so, if you wanted to match ItemSlots that both had BellPeppers, for example, you could write

    instance Eq ItemSlot where
        ItemSlot BellPepper a == ItemSlot BellPepper a' = True
        -- or, equivalently
        ItemSlot { item = BellPepper } == ItemSlot { item = BellPepper } = True
    

    though usually you would delegate the comparison of Item‘s to the Eq instance for Items.

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

Sidebar

Related Questions

I'm learning Haskell and stuck trying to understand the type system. I'm trying to
While learning an example from learn-you-a-haskell which right triangle that has integers for all
I'm still learning Haskell and need help with the type inference please! Using packages
I am currently learning Haskell. I am trying to write a function which given
I'm learning Haskell, and these are my first steps in trying to understand the
I am learning Haskell and reading the book, Learn You a Haskell for Great
I'm trying to broaden my mind by learning Haskell. My self-inflicted homework was to
I've recently been learning Haskell, and I noticed that the String type (or [Char]
I'm learning how to use arrays in Haskell, for example, generating a times table:
I'm just learning Haskell, and trying to figure out the most idiomatic way to

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.