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

  • Home
  • SEARCH
  • 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 8512487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:17:30+00:00 2026-06-11T04:17:30+00:00

This is my solution to exercise from YAHT: Exercise 4.6 Write a datatype Tuple

  • 0

This is my solution to exercise from YAHT:

Exercise 4.6 Write a datatype Tuple which can hold one, two, three or
four elements, depending on the constructor (that is, there should be
four constructors, one for each number of arguments). Also provide
functions tuple1 through tuple4 which take a tuple and return Just the
value in that position, or Nothing if the number is in valid (i.e.,
you ask for the tuple4 on a tuple holding only two elements).

When I wrote a first line I was excited about simplicity comparing to C#


    data Tuplex a b c d = Tuple1 a | Tuple2 a b | Tuple3 a b c | Tuple4 a b c d

    -- class Tuplex<a,b,c,d> {
    --       Tuplex(a p1){ _p1 = p1; }
    --       Tuplex(a p1, b p2){ _p1 = p1; _p2 = p2; }
    --       Tuplex(a p1, b p2, c p3){ _p1 = p1; _p2 = p2; _p3 = p3; }
    --       Tuplex(a p1, b p2, c p3, d p4){ _p1 = p1; _p2 = p2; _p3 = p3; _p4 = p4; }
    --       public Nullable<a> _p1;
    --       public Nullable<b> _p2;
    --       public Nullable<c> _p3;
    --       public Nullable<d> _p4;
    -- }

In C# I can access any field without problem, but here I should write an ‘accessor functions’, right? And amount of code here makes me sad.

Can I have shorter code here?


    tuple1 ∷ Tuplex a b c d → Maybe a
    tuple2 ∷ Tuplex a b c d → Maybe b
    tuple3 ∷ Tuplex a b c d → Maybe c
    tuple4 ∷ Tuplex a b c d → Maybe d
    tuple1 (Tuple1 a) = Just a
    tuple1 (Tuple2 a b) = Just a
    tuple1 (Tuple3 a b c) = Just a
    tuple1 (Tuple4 a b c d) = Just a
    tuple2 (Tuple1 a) = Nothing
    tuple2 (Tuple2 a b) = Just b
    tuple2 (Tuple3 a b c) = Just b
    tuple2 (Tuple4 a b c d) = Just b
    tuple3 (Tuple1 a) = Nothing
    tuple3 (Tuple2 a b) = Nothing
    tuple3 (Tuple3 a b c) = Just c
    tuple3 (Tuple4 a b c d) = Just c
    tuple4 (Tuple1 a) = Nothing
    tuple4 (Tuple2 a b) = Nothing
    tuple4 (Tuple3 a b c) = Nothing
    tuple4 (Tuple4 a b c d) = Just d

    -- unit tests
    prop_tx1 = tuple1 (Tuple1 4) ≡ Just 4
    prop_tx2 = tuple1 (Tuple2 4 'q') ≡ Just 4
    prop_tx3 = tuple2 (Tuple1 4) ≡ (Nothing ∷ Maybe Char)
    prop_tx4 = tuple2 (Tuple2 4 'q') ≡ Just 'q'

  • 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-11T04:17:31+00:00Added an answer on June 11, 2026 at 4:17 am

    I would try to keep it dead simple:

    data Tuplex a b c d = Tuple1 a | Tuple2 a b | Tuple3 a b c | Tuple4 a b c d
    
    toMaybes (Tuple1 p)       = (Just p, Nothing, Nothing, Nothing)
    toMaybes (Tuple2 p q)     = (Just p, Just  q, Nothing, Nothing)
    toMaybes (Tuple3 p q r)   = (Just p, Just  q, Just  r, Nothing)
    toMaybes (Tuple4 p q r s) = (Just p, Just  q, Just  r, Just  s)
    
    tuple1 t = p where (p,_,_,_) = toMaybes t 
    tuple2 t = q where (_,q,_,_) = toMaybes t 
    tuple3 t = r where (_,_,r,_) = toMaybes t 
    tuple4 t = s where (_,_,_,s) = toMaybes t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is one of my homework exercise. Write a shell program, which will take
I'm learning quicksort from Sedgwick's book. One of his exercise problems is Write a
First of all, this solution is no option for me, because I can't change
I have a table with two fields of interest for this particular exercise: a
I am learning python atm, and was doing an exercise from this site --
This exercise was a bit tricky. Figured I'd post my solution to see if
The followng snippet contains a solution for exercise 3 on page 69 (write a
This exercise is taken from Google's Python Class : D. Given a list of
is this solution possible in asp.net dragging picturebox inside winform on runtime i just
This was just what I was thinking for this solution and I would like

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.