Binary Thoughts
Binary Thoughts
Day #5: Functional Programming with Python.

Day #5: Functional Programming with Python.

Manish Reddy Nandineni's photo
Manish Reddy Nandineni
·Jun 15, 2020·

3 min read

Hey. Hope you have been enjoying the series so far. Today is the Day 5 of the #100DaysOfCodeChallenge. Received a problem previously asked by Jane Street (refer the hyperlink for the company details) with a medium tag to it. This question is a true puzzle and can be solved in a minute if your functional programming understanding is strong.

You may get confused with the question, just like the way I did. I will try my best in putting the solution in the simplest way possible. You can always reach out to me if you need help.

PermalinkThe Question On Day #5:

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.

PermalinkExplanation

The question is a bit confusing but let us try to understand it clearly.

  • cons is a function which takes two inputs (consider integers). It returns a value which is an function defined inside itself, i.e. pair.
  • pair is a function which takes a function f as input and returns the return value of its input function, i.e. the return value of the function f is returned by pair.
  • Now f is a function which will return a value.
  • f is the key to all our solution.

PermalinkAlgorithm

  • The end value obtained on calling the function cons is a function pointer pointing to the pair function.
  • The required implementation goes as follows:

PermalinkPython Code

Output 3 4

PermalinkExplanation

When car(cons(3,4)) is called,

  1. cons(3,4) is invoked in the first place.
  2. cons returns the function pair function.
  3. The parameter passed to the car function is the pair function.
  4. car function returns the value returned by calling the pair function.
  5. pair function takes a function as input, so we pass the left function to it.
  6. pair function returns the value obtained by passing a, b to a a function f. So, our left is the function f, which takes a, b as input and returns a as output to the pair function.
  7. Now the same a is returned by pair function to the car function, which will again return the same value of a.
  8. Now a is returned at the end and printed.
  9. Same goes with the function cdr.

#The Python visualiser is very much needed to understand this logic more clearly, please refer it here.

I hope you were able to understand this problem. Feel free to reach out for any query clearance.

Thanks and cheers:)

Subscribe to our newsletter

Read articles from Binary Thoughts directly inside your inbox. Subscribe to the newsletter, and don't miss out.

 
Share this

Article Series

100 Days Of Code

1

Day #1: My Start With 100 Days Of Code

Hi! I'm Manish. I was wondering what to do in this COVID-19 pandemic, to stay a bit productive and t…

Day #1: My Start With 100 Days Of Code
2

Day #2: Product of all the elements of the array, except self.

Hello, today is the Day 2 of the #100DaysOfCodeChallenge. Received a problem previously asked by Ube…

Day #2: Product of all the elements of the array, except self.
4

Day #4: Finding the smallest positive missing integer from an unordered list using Python.

Hey, today is the Day 4 of the #100DaysOfCodeChallenge. Received a problem previously asked by Strip…

Day #4: Finding the smallest positive missing integer from an unordered list using Python.
5

Day #5: Functional Programming with Python.

Hey. Hope you have been enjoying the series so far. Today is the Day 5 of the #100DaysOfCodeChalleng…

Day #5: Functional Programming with Python.
6

Day #6: XOR Doubly Linked List

Hello. Today is the Day 6 of the #100DaysOfCodeChallenge. Received a problem previously asked by Goo…

Day #6: XOR Doubly Linked List
7

Day #7: Number of possible decodable messages

Hello everyone. Today is the Day 7 of the #100DaysOfCodeChallenge. And it's a week!!!! Received a pr…

Day #7: Number of possible decodable messages
8

Day #8: Number Of Unival Trees Using Python

Hello everyone. Today is the Day 8 of the #100DaysOfCodeChallenge. Received a problem previously ask…

Day #8: Number Of Unival Trees Using Python
Binary Thoughts

©2025 Binary Thoughts

Archive·Privacy policy·Terms

Powered by Hashnode - Build your developer hub.

Start your blogCreate docs

Blog author

Subscribe to our newsletter

Stay in the loop! Get new articles from Binary Thoughts delivered straight to your inbox.

Continue with GoogleMore options