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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:49:21+00:00 2026-06-02T12:49:21+00:00

Say I have something silly like this: data SomeType = Unary Int | Associative

  • 0

Say I have something silly like this:

data SomeType
    = Unary Int
    | Associative SomeType
    | Binary SomeType SomeType

some_func :: SomeType -> Int
some_func s =
    case s of
        Unary n -> n
        Associative s1 -> some_func s1
        Binary s1 s2 -> some_func s1 + some_func s2

Here some_func will look through all SomeTypes in a given SomeType and sum up the Ints of all Unary data constructors. SomeType is a recursive data type.

In these pattern matches I’m repeating some_func s1. Is there a way to use @, when, let or anything else to declare sf1 = some_func s1 and use it in both? Something like this:

data SomeType
    = Unary Int
    | Associative SomeType
    | Binary SomeType SomeType

some_func :: SomeType -> Int
some_func s =
    case s of
        Unary n -> n
        Associative s1 -> sf1
        Binary s1 s2 -> sf1 + sf2
        where
            sf1 = some_func s1
            sf2 = some_func s2

The problem here is that s1 and s2 are only known in the block after -> and sf1 can’t be calculated.

  • 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-02T12:49:23+00:00Added an answer on June 2, 2026 at 12:49 pm

    Abuse record syntax!

    data SomeType
        = Unary { u :: Int }
        | Associative { s1 :: SomeType }
        | Binary { s1, s2 :: SomeType }
    
    someFunc :: SomeType -> Int
    someFunc s = case s of
        Unary{}       -> u s
        Associative{} -> sf1
        Binary{}      -> sf1 + sf2
      where
        sf1 = someFunc (s1 s)
        sf2 = someFunc (s2 s)
    

    Note that different constructors of the same type are allowed to have the same named fields in their records. Laziness prevents you from erroring on sf2 if you go down the Associative branch.

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

Sidebar

Related Questions

Say I have something like this, which sends unhanded exceptions to logging.critical() : import
Let say I have something like this: id | title | 1 | First
Let's say I have something like this: $db=new PDO($dsn); $statement=$db->query('Select * from foo'); while
Let's say I started a thread and I have something like this: ...//initiate all
This may be a silly question but... Say you have a sentence like: The
Lets say I have something like this: http://gyazo.com/642987562175afc6d11a962762327744.png?1329570534 Basically, the user fills out this
Let's say I have something like this: d = { abc : [1, 2,
let's say we have something like this public class Person { public string Name
Let's say I have something like this (this is only an example, actual request
Say I have something like this <script type=text/javascript src=somefile.js></script> <div>something</div> <script type=text/javascript> // Some

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.