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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:19:00+00:00 2026-06-13T12:19:00+00:00

I wrote the below code to construct a tree with given vertex given a

  • 0

I wrote the below code to construct a tree with given vertex given a list of connections between vertices.

type Connection = (Int,Int)
data Tree = Leaf Int | Node Int [Tree] deriving (Eq,Read,Show)

makeTree :: Int -> [Connection] -> Tree
makeTree x [] = Leaf x
makeTree indx connections =  Node indx otherTrees where
  otherTrees = [makeTree i cx | i <- directConnections, let cx = removeConnectionsTo indx connections]
  directConnections = map (\(x,y) -> if (x == indx) then y else x) $ filter (\(x,y) -> x == indx || y   == indx) connections

removeConnectionsTo :: Int -> [Connection] -> [Connection]
removeConnectionsTo indx = filter (\(x,y) ->    x /= indx && y /= indx)

For some reason, the inputs below give me surprisingly different results:

makeTree 1 [(1,2),(1,3)] gives me Node 1 [Leaf 2,Leaf 3]

makeTree 1 [(1,2),(1,5),(2,3),(2,4),(5,6),(5,7)] gives me Node 1 [Node 2 [Node 3 [],Node 4 []],Node 5 [Node 6 [],Node 7 []]]

I am running GHCi, version 7.4.1 on OS X 10.8.2.

I don’t understand why I get Leaf twice in the first example (correct) but node with empty subtree lists in the second example (incorrect).

  • 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-13T12:19:00+00:00Added an answer on June 13, 2026 at 12:19 pm

    A quick fix would be to just check if otherTrees is empty before deciding whether to build a Leaf or a Node, e.g.

    makeTree indx connections
      | null otherTrees = Leaf indx
      | otherwise       = Node indx otherTrees
      where ...  
    

    To understand what is happening here, let’s add a little instrumentation:

    import Debug.Trace
    
    makeTree :: Int -> [Connection] -> Tree
    makeTree ix cs | traceShow (ix, cs) False = undefined
    makeTree x [] = ... -- leave rest of the function as before
    

    Now load it up into GHCi, and let’s see what the recursive calls are:

    > import Control.DeepSeq
    > (show $ makeTree 1 [(1,2),(1,5),(2,3),(2,4),(5,6),(5,7)]) `deepseq` ()
    (1,[(1,2),(1,5),(2,3),(2,4),(5,6),(5,7)])
    (2,[(2,3),(2,4),(5,6),(5,7)])
    (3,[(5,6),(5,7)])
    (4,[(5,6),(5,7)])
    (5,[(2,3),(2,4),(5,6),(5,7)])
    (6,[(2,3),(2,4)])
    (7,[(2,3),(2,4)])
    ()
    

    As you can see, the list in the second argument is not empty which is why it doesn’t match the first case of your function, so you’ll either need to add some additional checks as in my example, or make sure that you filter out the rest of the connections.

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

Sidebar

Related Questions

I have to perform async execution, and i have wrote the below code var
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
So I wrote the code below: jQuery('#contentWrapper').delegate(.addButton, click, function(){ addRow(jQuery(this)); }); function addRow(thisButton){ var
I have wrote the code below which was taken from Java How to program
Below is the code which I wrote to display a tooltip content. As the
Below is some jQuery code the i wrote to make a simple animation. However
Below given code is for video streaming. This is fine with IE9 and firefox
Help me please with Boruvka algorithm for creating a minnimum-spanning tree. I wrote code
i write below code for sending id to another php file function selectCheckBox(k) {
i wrote the code belwo for url-mapping : <urlMappings enabled=true> <add url=~/My Fantastic Page

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.