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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:06:30+00:00 2026-05-27T07:06:30+00:00

I was trying to translate the following Haskell code to C++: data List t

  • 0

I was trying to translate the following Haskell code to C++:

data List t = Nil | Cons t (List t)

The straightforward translation of the algebraic data type to the stateless Visitor pattern yields the following Java code

interface List<T> {
  <R> R accept(ListVisitor<T,R> v);
}

interface ListVisitor<T,R> {
  R visitNil();
  R visitCons(T head, List<T> tail);
}

class Nil<T> implements List<T> {
  @Override
  public <R> R accept(ListVisitor<T,R> v) {
    return v.visitNil();
  }
}

class Cons<T> implements List<T> {
  public final T head;
  public final List<T> tail;
  public Cons(T head, List<T> tail) {
    this.head = head;
    this.tail = tail;
  }
  @Override
  public <R> R accept(ListVisitor<T,R> v) {
    return v.visitCons(head, tail);
  }
}

The following is the C++ code I have so far:

template<class T> class List;

template<class T, class R> class ListVisitor {
  virtual R visitNil() = 0;
  virtual R visitCons(T head, List<T> tail) = 0;
};

template<class T> class List {
  template<class R> virtual R accept(ListVisitor<T,R> v) = 0;
};

Note that the Java version uses a virtual generic function accept. When I translate it to C++, I end up with a virtual template function, which is not allowed by C++.

Is there a solution to it other than making accept return void and require visitors to be stateful?

Update:
As requested, here are some examples of how the interfaces could be used (modulo smart pointers and possible compile errors):

template<class T> struct LengthVisitor : ListVisitor<T, int> {
  bool visitNil() { return 0; }
  bool visitCons(const T&, const List<T> &tail) { return 1 + tail.accept(*this); }
};

template<class T> struct ConcatVisitor : ListVisitor<T, const List<T> *> {
  const List<T> *right;
  ConcatVisitor(const List<T> *right) : right(right) {} 
  List<T> * visitNil() { return right; }
  List<T> * visitCons(const T &head, const List<T> & tail) {
    return new Cons(head, tail.accept(*this));
  }
};

Another example, a higher-level function fold, in Java, can be found here: http://hpaste.org/54650

  • 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-27T07:06:31+00:00Added an answer on May 27, 2026 at 7:06 am

    This can certainly be improved (use smart pointers for tail ownership, for example), but the basic idea:

    template <typename T>
    struct cons_list {
         T head;
         cons_list<T>* tail;
    
         explicit cons_list(T head, cons_list *tail = nullptr)
             : head(head), tail(tail) {}
    
         template <template<typename> class Visitor>
         typename Visitor<T>::return_type accept(const Visitor<T>& visitor) {
              return visitor.visit(head, tail);
         }
    };
    
    template <typename T>
    struct some_visitor {
         typedef void return_type;
    
         return_type visit(T head, cons_list<T>* tail) const {
              std::cout << head << '\n';
              if (tail != nullptr) tail->accept(*this);
         }
    };
    

    Demo. No need for virtual dispatch and class hierarchies. nullptr is C++11, but it should work just fine on 03.

    It might be a better idea to implement accept as free function, and not use null pointers as nil node, but as I said, that’s the basic thing.

    Note: this is more-or-less the idea behind boost::static_visitor.

    A full C++11 Boost.Variant version (needs template aliases). Not tested, because I don’t have g++ 4.7 nearby.

    struct nil_node {};
    template <typename T> cons_node;
    
    template <typename T>
    using cons_list = boost::make_recursive_variant<
         nil_node, cons_node<T>
    >::type;
    
    template <typename T>
    struct cons_node {
         T head;
         cons_list<T> tail;
    
         explicit cons_node(T head, const cons_list<T>& tail)
             : head(head), tail(tail)
         {}
    };
    
    template <typename T>
    struct some_visitor : boost::static_visitor<T> {
         void operator()(nil_node&) {}
         void operator()(cons_node<T>& node) {
             std::cout << node.head << '\n';
             boost::apply_visitor(node.tail, *this);
         }
    };
    
    int main() {
        cons_node<int> x(1, cons_node<int>(2, cons_node<int>(3, nil_node())));
        boost::apply_visitor(some_visitor<int>(), x);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for some reasons I am trying to translate the following RoR view code to
I'm trying to translate the following code from C++ to C# ` struct tPacket
when trying to translate the confirmation message to Norwegian i get the following error:
Sometimes I still get stuck trying to translate procedural code into functional code. Is
I have some jquery code which I am trying to translate to YUI. I
I have the following MySQL query that I'm trying to translate into an equivalent
I'm trying to translate the following from AT&T assembly to Intel assembly: pushl 2000
I have the following SQL, which I am trying to translate to LINQ: SELECT
I'm trying to parse the following URI : http://translate.google.com/#zh-CN|en|你 but got this error message
I am trying to use Google Translate REST API and while requesting the following

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.