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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:58:58+00:00 2026-06-12T16:58:58+00:00

In Haskell I can define following data type: data Tree = Empty | Leaf

  • 0

In Haskell I can define following data type:

data Tree = Empty
      | Leaf Int
      | Node Tree Tree

and then write polymorphic function like this:

depth :: Tree -> Int
depth Empty = 0
depth (Leaf n) = 1
depth (Node l r) = 1 + max (depth l) (depth r)

In Java I can emulate algebraic data types with interfaces:

interface Tree {}
class Empty implements Tree {}
class Leaf implements Tree { int n; }
class Node implements Tree { Tree l; Tree r; }

But if I try to use Haskell-like polymorphism, I get an error:

int depth(Empty node) {
    return 0;
}
int depth(Leaf node) {
    return 1;
}
int depth(Node node) {
    return 1 + Math.max(depth(node.l), depth(node.r));   // ERROR: Cannot resolve method 'depth(Tree)'
}

Correct way to overcome this is to put method depth() to each class. But what if I don’t want to put it there? For example, method depth() may be not directly related to Tree and adding it to class would break business logic. Or, even worse, Tree may be written in 3rd party library that I don’t have access to. In this case, what is the simplest way to implement ADT-like polymorpism?

Just in case, for the moment I’m using following syntax, which is obviously ill-favored:

int depth(Tree tree) {
    if (tree instanceof Empty) depth((Empty)tree)
    if (tree instanceof Leaf) depth((Leaf)tree);
    if (tree instanceof Node) depth((Node)tree); 
    else throw new RuntimeException("Don't know how to find depth of " + tree.getClass());
}
  • 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-12T16:58:59+00:00Added an answer on June 12, 2026 at 4:58 pm

    Try something like this.

    Sorry, my Java is very rusty. If, unlike me, you can remember the syntax, you could use Java generics to refine Object to Integer or whatever class the method you’re writing needs. But you can’t (can you?) return primitive types, sorry.

    interface TreeFolder {
        Object onEmpty();
        Object onLeaf (int n);
        Object onNode (Tree l, Tree r);
    }
    
    interface Tree {
        Object fold (TreeFolder f);
    }
    
    class Empty implements Tree {
        Object fold (TreeFolder f) {
            return f.onEmpty();
        }
    }
    
    class Leaf implements Tree {
        private int n;
        Object fold (TreeFolder f) {
            return f.onLeaf (n);
        }
    }
    
    class Node implements Tree {
        private Tree l, r;
        Object fold (TreeFolder f) {
            return f.onNode (l, r);
        }
    }
    
    // meanwhile, in a class in another package far far away...
    Object depth (Tree tree) {
        return tree.fold (new TreeFolder() {
            Object onEmpty() { return new Integer(0); }
            Object onLeaf (int n) { return new Integer(n); }
            Object onNode (Tree l, Tree r) {
                Integer ll = (Integer) l.fold (this);
                Integer rr = (Integer) r.fold (this);
                return new Integer (ll.intValue() + rr.intValue());
            }
        });
    }
    

    Note that in depth() I have to manually recurse (call fold()) on the Tree parameters. You could instead choose to recurse on them upfront in Node.fold() (and change TreeFolder accordingly), but then you have to recurse — you can’t choose to recurse only into the left subtree, should you wish to. (In Haskell we don’t have to make that trade-off thanks to laziness.)

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

Sidebar

Related Questions

In Haskell I can easily define a recursive function which takes a value and
High Guys, I have to define a polymorphic datatype for a tree that can
I'd like to define a function which can show values of any type, with
I've defined a Tree data type in Haskell and an associated 'size' method which
I am trying to define a Vector3 data type in Haskell and allow the
In Haskell, is it possible to write a function with a signature that can
I'm trying to understand type patterns and generic classes in Haskell but can't seem
Can 2 or more equations defining a function in Haskell share the same where
A question arised when I was programming a recursive function in Haskell; Can any
I want to define a Haskell function that removes from a list of strings

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.