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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:04:24+00:00 2026-05-18T00:04:24+00:00

Haskell here we go! Okay so here is an example of the code currently

  • 0

Haskell here we go! Okay so here is an example of the code currently used:

data Tree a = Node (Tree a) (Tree a) | Leaf a | Empty  deriving (Show)
main = do
let my_tree = Node (Node (Leaf [1,2,4,9]) (Leaf [3,5,5,8,5])) (Leaf [2,7,5,5,2])
cc my_tree
bb my_tree  
aa my_tree
cc my_tree  
print("Done")

Simply creating a tree that holds Int lists and need 3 functions to work aa,bb,cc:

cc – simply outputs a tree
bb – ads to each value +2
aa – outputs a list for each value like: [length,minimum, maximum]

now there is a code that does all this almost perfectly, I put it in codepad (ask me if you want): http://codepad.org/???????
now as you can see the last output of aa is also a tree
its probably a quick fix but I cant figure it out – so that aa would output not a tree but a list [] of these:

[length param,minimum param,maximum param]

in other words, does someone know how to output it as a list, not inside tree??
so in output instead of:

Node (Node (Leaf [4,1,9]) (Leaf [6,0,8])) (Leaf [5,2,7])

would be

[[4,1,9],[6,0,8],[5,2,7]]

I believe something has to be modified here:

fmap3 ff (Node l r) = Node (fmap2 ff l) (fmap2 ff r)
fmap3 ff (Leaf x) = Leaf (ff x) 

Anyone ??

  • 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-18T00:04:25+00:00Added an answer on May 18, 2026 at 12:04 am

    You’re trying to perform a reduce operation: turn a tree into a single value (three times: length, maximum, minimum). This cannot be done using a map function, because a map by definition always turns a tree into a tree.

    A typical reduce algorithm uses a function to “merge” together two reduced results, and recursively merges the results obtained anywhere in the tree. So, you’ll be having something like this:

    reduce ff (Node l r) = ff (reduce ff l) (reduce ff r)
    reduce ff (Leaf x)   = x
    

    Once this is done, you can use a map function to turn your tree of (whatever) into a tree of values to be reduced, and apply the reduce function.

    cc tree = [ reduce (+)   (fmap2 length tree) , 
                reduce (min) (fmap2 minimum tree) ,
                reduce (max) (fmap2 maximum tree) ]
    

    EDIT: you’ve changed your question while I was answering. You can do this using the above reduction function and the list concatenation function ++, but list concatenation is not very sexy in terms of performance.

    There’s an idiomatic way to turn a tree into a list by using a traversal with an accumulator argument:

    traverse init (Node l r) = traverse (traverse init r) l
    traverse init (Leaf x)   = x : init
    
    cc tree = traverse [] (fmap2 h tree)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning Haskell, Which language (F# or Haskell) do you prefer for programming
I'm currently working on project with Haskell, and have found myself some trouble. I'm
Haskell is givinig me a headache today. I want to handle an exception. When
In Haskell, is there a way to restrict a monad M a so that
I know a Haskell module name, but I can't figure out in what package
From the haskell report: The quot, rem, div, and mod class methods satisfy these
In reading Haskell-related stuff I sometimes come across the expression tying the knot, I
I was reading a research paper about Haskell and how HList is implemented and
I'm writing some disposable Haskell scripts to solve some of the Project Euler problems.
I am a bit rusty on my Haskell and am looking to ramp back

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.