Hacker News new | past | comments | ask | show | jobs | submit login

struct List {}; struct Cons : public List { Cons(int v, List* nxt); int v; List* nxt; }; struct Nil : public List {};

This is how you'd do it in Java.




and you would use it ... how?


I've showed you an alternative encoding, maybe get to work and learn something yourself?


It's not exactly Java, but:

    Cons(1, Cons(2, Cons(3, Nil)))


Actually, Java does have sum types nowadays, and you can write a recursive list implementation like “new Cons(2, new Cons(3, Nil.nil()))”.

Here is a definition for a Maybe type, converting it to a List is left as an exercise to the reader :) : https://news.ycombinator.com/item?id=35133670


I just meant because I was eliding the `new`s.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: