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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:57:27+00:00 2026-05-28T18:57:27+00:00

As an exercise, I am implementing in Haskell a ‘cons’ operation that forms a

  • 0

As an exercise, I am implementing in Haskell a ‘cons’ operation that forms a pair from two values of any type. Implementing the needed data type is easy enough:

data Nil = Nil deriving (Eq)
data Pair a b = Cons a b deriving (Eq)

car (Cons x _) = x
cdr (Cons _ y) = y

caar = car . car
cdar = cdr . car
cadr = car . cdr
cddr = cdr . cdr

*Main> cddr (Cons 55 (Cons (1,2,3,4) "hello, world!"))
"hello, world!"
*Main> 

but inspired by this thread, I want to make the resulting pairs print out like Scheme lists would – including the infamous “improper list” (1 2 3 . 4). My implementation (see below) is working for Char’s:

*Main> Cons 'a' (Cons 'b' (Cons 'c' Nil))
('a' 'b' 'c')
*Main> Cons 'a' (Cons 'b' 'c')
('a' 'b' . 'c')
*Main> Cons (Cons 'a' 'b')(Cons 'c' (Cons 'd' Nil))
(('a' . 'b') 'c' 'd')

It’s not working so well for Int’s (or any other data type). So my question is: how can I make this work for other data types? i.e., I want it to work like this:

*Main> Cons 5 (Cons "hello" (Cons False Nil))
(5 "hello" False)

My current full implementation follows:

data Nil = Nil deriving (Eq)
data Pair a b = Cons a b deriving (Eq)

car (Cons x _) = x
cdr (Cons _ y) = y

caar = car . car
cdar = cdr . car
cadr = car . cdr
cddr = cdr . cdr

instance Show Nil where show _ = "()"

class ShowPair a where
  showRight::a->String

instance (Show a, ShowPair a, ShowPair b)=>Show (Pair a b) where
  show (Cons car cdr) = "(" ++ (show car) ++ (showRight cdr) ++ ")"

instance (Show a, ShowPair a, ShowPair b)=>ShowPair (Pair a b) where
  showRight (Cons car cdr) = " " ++ (show car) ++ (showRight cdr)

instance ShowPair Char where
  showRight x = " . " ++ show x

instance ShowPair Int where
  showRight x = " . " ++ show x

instance ShowPair Nil where
  showRight _ = ""
  • 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-05-28T18:57:28+00:00Added an answer on May 28, 2026 at 6:57 pm

    Here’s an option. First, enable these extensions by putting this line at the top of your file:

    {-# LANGUAGE FlexibleInstances, OverlappingInstances, UndecidableInstances#-}
    

    Next, remove your ShowPair instances for Char and Int.

    Now add a ShowPair instance for anything with Show:

    instance Show a => ShowPair a where showRight = (" . " ++) . show
    

    This now ensures that any type a which is an instance of Show is also an instance of ShowPair where it is shown by prepending a . to its normal string form. However, if a type has a more specific ShowPair instance (e.g. Nil), Haskell will use that one instead.

    This is not part of standard Haskell, so you need to enable the three language extensions. Look at How to write an instance for all types in another type class? for more information on why you need the extensions.

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

Sidebar

Related Questions

i created an exercise program that create a dynamic menu from json. and then,
As an exercise I wrote a short Haskell function that returns the first four
This is an academical exercise (disclaimer). I'm building an application that will profit from
As part of an exercise, am implementing an ArrayList which will support Enumerations. Following
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
As an exercise, and mostly for my own amusement, I'm implementing a backtracking packrat
I'm doing a book exercise that says to write a program that generates psuedorandom
I'm looking few exercise from university about C++ and I found out this exercise:
I have started learning Haskell from Learn You a Haskell . In one of
As part learning exercise, part hobby project, I am implementing my own interpretation of

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.