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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:51:08+00:00 2026-05-26T02:51:08+00:00

Given data BTree a = End | Node a (BTree a) (BTree a) deriving(Show,Eq,Ord)

  • 0

Given

data BTree a = End
             | Node a (BTree a) (BTree a)
   deriving(Show,Eq,Ord)

data Msg = Msg { from :: String
               , to :: String
               , when :: Int
               , message :: String }

instance Ord Msg where
    compare a b = (when a) `compare` (when b)

instance Eq Msg where
    (==) a b = (when a) == (when b)

My function to count nodes (which seems off but that’s aside from the question) is

count :: (Ord a) => (BTree a) -> Int
count = sum . count'
 where
  count' :: (Ord a) => (BTree a) -> [Int] 
  count' End = []
  count' (Node _ l r) =
    [1] ++ (count' l) ++ (count' r)

Does count not evaluate the contents of the Msg by virtue of its value being discarded by _? Perhaps a better question is, how do I know where lazy evaluation starts and ends for this sort of thing?

If the third line of count' was:

count' (Node (Msg x _ _ _) l r) =

Can I assume that the other three fields of Msg were accessed/evaluated, or does lazy evaluation go that far?

  • 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-26T02:51:08+00:00Added an answer on May 26, 2026 at 2:51 am

    No. The fields of a data structure are evaluated lazily by default. Since you’re not using the other fields in any way, they will not be evaluated by this code. If you want to make it so that evaluating a node forces all its fields to be evaluated, you can add strictness annotations to the fields:

    data BTree a = End
                 | Node !a (BTree a) (BTree a)
       deriving(Show,Eq,Ord)
    
    data Msg = Msg { from :: !String
                   , to :: !String
                   , when :: !Int
                   , message :: !String }
    

    Since counting the nodes forces the nodes themselves to be evaluated, this will also force the node values to be evaluated. If you only want this behavior for your one function, you can force evaluation in a more fine-grained manner using seq:

    count' (Node x l r) = x `seq` ([1] ++ count' l ++ count' r)
    

    or a bang pattern (requires the BangPatterns extension)

    count' (Node !x l r) = [1] ++ count' l ++ count' r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A query (see below) that extracts climate data from weather stations within a given
I'm given data that could be either a string or an array. I need
Given data encoded as a Base64-encoded string, can I somehow calculate the actual length
I am given data from an API in a format that is pretty difficult
Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957
Given a data structure (e.g. a hash of hashes), what's the clean/recommended way to
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
Given a data stream of continuously arriving items containing a timestamp and text (e.g.
I'm being given a data source weekly that I'm going to parse and put
I know you could make a helper pretty easily given the data. So, if

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.