rbucinell / ProgrammingChallenges

Code repository for my programming challenges
0 stars 0 forks source link

Daily Coding Problem: Problem #5 #3

Open rbucinell opened 6 years ago

rbucinell commented 6 years ago

This problem was asked by Jane Street.

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.

neonb88 commented 5 years ago

@rbucinell Do you understand what it's asking? It seems like they're passing a function pointer as a parameter to pair(), but then it just returns the function pointer to pair as the return value for cons(). Confused why they would implement it this way

rbucinell commented 5 years ago

@neonb88 Yeah I do, this references a paradigm in the Scheme programming language. The coding problem is basically asking you to re-implement car and cdr from that language. car being the first element in a pair/list, and cdr containing the next set.

It was a fun side language to do in college. It really helps with your recursion practice since there are not traditional loops.