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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:39:41+00:00 2026-06-07T08:39:41+00:00

This is supposed to be really simple, but I cannot seem to get around

  • 0

This is supposed to be really simple, but I cannot seem to get around it.

Suppose I have my own List class, declaring head and tail in its interface. List is supposed to be what you expect, that is a collection of homogeneous items.
Then, I want to create a data type implementing the List interface.

The following code is what I came up with, but it does not work: how would you fix it?

class List l where                                                             
    head :: l -> a  -- ERROR! How do I tell: given a list, return an element?                                                      
    tail :: l -> l                                                             

data ConsList a = Nil | Cons a (ConsList a)                                    

instance List (ConsList Int) where                                             
    head Nil = error "Empty List"                                              
    head (Cons h _) = h                                                        
    tail Nil = error "Empty List"                                              
    tail (Cons _ t) = t                       

Thanks in advance!

  • 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-07T08:39:44+00:00Added an answer on June 7, 2026 at 8:39 am

    Rather than defining List a type class, define it as a constructor class:

    class List l where
        head :: l a -> a
        tail :: l a -> l a                                        
    
    data ConsList a = Nil | Cons a (ConsList a)
    
    instance List ConsList where
        head Nil = error "Empty List"
        head (Cons h _) = h
        tail Nil = error "Empty List"
        tail (Cons _ t) = t
    

    Alternatively, fix the element type (note: for your type ConsList, this requires flexible instances):

    {-# LANGUAGE FlexibleInstances #-}
    
    class List l where
        head :: l -> Int
        tail :: l -> l
    
    data ConsList a = Nil | Cons a (ConsList a)
    
    instance List (ConsList Int) where
        head Nil = error "Empty List"
        head (Cons h _) = h
        tail Nil = error "Empty List"
        tail (Cons _ t) = t
    

    Finally, with type families you can do even more fancy stuff, but it really depends on your specific scenario if you should go that far (probably not):

    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE TypeFamilies      #-}
    
    class List l where
        type Elt l
        head :: l -> Elt l
        tail :: l -> l
    
    data ConsList a = Nil | Cons a (ConsList a)
    
    instance List (ConsList Int) where
        type Elt (ConsList Int) = Int
    
        head Nil = error "Empty List"
        head (Cons h _) = h
    
        tail Nil = error "Empty List"
        tail (Cons _ t) = t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A newbie question,really. Suppose I have a html table like this: <div id=div1> <table
This should be really simple, but I can't figure it out. So: how do
I have a problem which I guessed would be really simple to solve... but
This may be a really simple regex but its one of those problems that
OK, this probably is supposed to be the easiest thing in the world, but
This piece of code is supposed to go through a list and preform some
I needed some really simple XML output so I decided to write my own
Suppose I have a custom collection class that provides some internal thread synchronization. For
I have a fairly simple method in a class in a separate file, which
This problem is very simple, but I just can't figure it out added 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.