Click here to Skip to main content
14,301,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. 
For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.
Given this implementation of cons:

def cons(a, b):
    def pair(f):
        return f(a, b)
    return pair
Implement car and cdr


What I have tried:

I have no idea on a solution for this one, i don't know what topic in java it falls under. This question was asked from a Python developer. Please help provide a solution. Thanks a lot.
Posted
Updated 5-Sep-19 19:12pm
v3

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It means "create a class that has a constructor taking two values a and b, and write two methods that return a and b respectively"

It's trivial, but it won't produce "good Java" code, since to be explicitly accurate to the request cons and cdr should be outside the class, and that's bad OOPs design.

Python isn't a real language: it's an accident looking for a place to happen. Tell him that, and watch him explode. It's true though.
   
Comments
UT7 6-Sep-19 1:18am
   
@OriginalGriff, wow, thanks a lot. I wouldn't tell him that, lol. Thank you.
OriginalGriff 6-Sep-19 1:52am
   
I would! :laugh:

https://www.codeproject.com/Surveys/2211/If-you-know-C-Cplusplus-Csharp-Objective-C-or-Java.aspx
UT7 6-Sep-19 2:02am
   
Seen, thanks.
phil.o 6-Sep-19 5:24am
   
Python isn't a real language: it's an accident looking for a place to happen.
I wouldn't be surprised if this one was placed in one's signature sooner or later :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100