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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:45:42+00:00 2026-05-11T01:45:42+00:00

I am reading a tutorial that uses the following example (that I’ll generalize somewhat):

  • 0

I am reading a tutorial that uses the following example (that I’ll generalize somewhat):

f :: Foo -> (Int, Foo) ... fList :: Foo -> [Int] fList foo = x : fList bar   where     (x, bar) = f foo 

My question lies in the fact that it seems you can refer to x and bar, by name, outside of the tuple where they are obtained. This would seem to act like destructuring parameter lists in other languages, if my guess is correct. (In other words, I didn’t have to do the following:)

fList foo = (fst tuple) : fList (snd tuple)       where         tuple = f foo 

Am I right about this behavior? I’ve never seen it mentioned yet in the tutorials/books I’ve been reading. Can someone point me to more info on the subject?

Edit: Can anything (lists, arrays, etc.) be destructured in a similar way, or can you only do this with tuples?

  • 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. 2026-05-11T01:45:43+00:00Added an answer on May 11, 2026 at 1:45 am

    Seeing your edit, I think what your asking about is Pattern matching.

    And to answer your question: Yes, anything you can construct, you can also ‘deconstruct’ using the constructors. For example, you’re probably familiar with this form of pattern matching:

    head :: [a] -> a head (x:xs) = x head []     = error "Can't take head of empty list" 

    However, there are more places where you can use pattern matching, other valid notations are:

    head xs = case xs of               (y:ys) -> y               []     -> error "Can't take head of empty list"  head xs = let (y:ys) = xs           in y  head xs = y   where     (y:ys) = xs 

    Note that the last two examples are a bit different from the first to because they give different error messages when you call them with an empty list.


    Although these examples are specific to lists, you can do the same with other data types, like so:

    first :: (a, b) -> a first tuple = x   where     (x, y) = tuple  second :: (a, b) -> b second tuple = let (x, y) = tuple                in y  fromJust :: Maybe a -> a fromJust ma = x   where     (Just x) = ma 

    Again, the last function will also crash if you call it with Nothing.

    To sum up; if you can create something using constructors (like (:) and [] for lists, or (,) for tuples, or Nothing and Just for Maybe), you can use those same constructors to do pattern matching in a variety of ways.

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

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer So it turns out what was happening was the following:… May 11, 2026 at 7:26 am
  • added an answer I would put the repeated elements in arrays and then… May 11, 2026 at 7:26 am
  • added an answer Checkout Merapi (http://www.merapiproject.net/) Java-to-AIR bridge. May 11, 2026 at 7:26 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.