+ 1
Does anyone know how can I implement the same Python code in Java?
6 Answers
+ 2
There is a project called Jython, an implementation of Python in written in Java. (See https://www.jython.org).
Hope that helps.
0
Just copy it and replace the Python keywords by the corresponding Java keywords. E.g. "def name" by "private static <return type> name" and so on. It's not too hard if you've already coded something in Java. Everything outside the def-section is moved into the main-method (here, in this small scope).
0
But in Java I can't create function inside a function.
Note that: when car is calling cons(3, 4) which returns left side(3) and when cdr calling the same cons(3, 4) which returns right side(4).
0
As I said, you move the content of the functions into methods and the rest into the main method. Then you finally have 4 methods: 1 main and your 3 functions.
0
I don't get it.. would you like to code it for me..
0
The problem is creating method inside a method.
I need to call a method cdr() which will take another method cons(3, 4) and return the right value, like this cdr(cons(3, 4)).
But when car method is called in the same way, i.e. car(cons(3, 4)) it should return left value.
Note that Python is very good at functional programming and data manipulation, where Java is object oriented and always need a return type.
I tried to return the values in class but that ended up returning only the reference of that data.