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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:34:32+00:00 2026-06-13T07:34:32+00:00

Using Haskell, I’m writing a function that counts the total number of leaves in

  • 0

Using Haskell, I’m writing a function that counts the total number of leaves in a tree. I have defined the tree as so:

data Tree a = Leaf | Node a (Tree a) (Tree a)

I writing a function that does this by:

countL :: Tree a -> Int
countL Leaf = 1
countL (Node x tL tR) = (countL tL) + (countL tR)

This works but I want to take it a step further by doing the same thing using the fold function. I’ve having a working fold function for trees that I defined by doing:

mytFold :: (a -> b -> b -> b) -> b -> Tree a -> b
mytFold f g Leaf = g
mytFold f g (Node a xl xr) = f a (mytFold f g xl) (mytFold f g xr)

I tried to include the fold function (also used a helper function that I defined by doing this:

countL' :: Tree a -> Integer
countL' Leaf = 1
countL' = mytFold leafy 0 where
        leafy tL tR = tL + tR

But I’m getting some weird errors. Does anyone have any insight on what’s wrong?

  • 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-13T07:34:34+00:00Added an answer on June 13, 2026 at 7:34 am

    There are two syntactic/type problems. Firstly, every top level binding must have the same number of arguments, so

    countL' Leaf = ..
    countL' = ..
    

    isn’t valid. One solution is

    countL' Leaf = ..
    countL' tree = mytFold leafy 0 tree
    

    Once you’ve done this, then GHC gives you an error like

        Couldn't match expected type `Integer -> Integer'
                    with actual type `Integer'
        Expected type: Integer -> Integer -> Integer -> Integer
          Actual type: Integer -> Integer -> Integer
        In the first argument of `mytFold', namely `leafy'
        In the expression: mytFold leafy 0 tree
    

    Which is because mytFold requires a 3-argument function as its first argument, and leafy just takes 2. Fix this by using leafy _ tL tR = tL + tR.


    However, once you have done this, you will see that this gives the wrong answer: countL' (Node 1 Leaf Leaf) == 0. A way that might make the solution clear is to removing the countL' Leaf case, and trying to write countL as just the fold. i.e.

    countL'' = mytFold f a
    

    where you have do decide what f and a are (hint: you already have f == leafy == const (+)). Consider mytFold leafy a Leaf.

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

Sidebar

Related Questions

I'm writing an audio program in Haskell using Portaudio. I have a function that
I want to implement a simple SceneGraph in Haskell using Data.Tree consisting of Transform
It's easy enough to represent a tree or list in haskell using algebraic data
I have this function in Haskell (I am using the Haskell-SDL library): pixel ::
I have been using Haskell for quite a while. The more I use it,
I'm writing a parallel Haskell program using Strategies. It's not doing what it's supposed
I'm trying to parse a binary format (PES) using Haskell: import qualified Data.ByteString.Lazy as
I am trying to produce an interactive Haskell program using the interact function with
Is there any XML-(de)serializer for Haskell using Data / Typeable with functions similar to
I have this parser for string parsing using Haskell Parsec library. myStringLiteral = lexeme

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.