Skip to main content

Daily Coding Problem 10MAY2020

I am subscribed to dailycodingproblem.com. From time to time I will share problems I attempt and have a discussion around it. My solution might have bugs or might not be the most efficient but it would be my attempt. Feel free to criticise.

Today's problem I got

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.

This was my solution in javascript. I am using the Javascript for Android app.

function cons(a,b) {
  return function pair(f) {
    return f(a,b);
  }
}

function car(pair) {
  function first(a,b) {
    return a;
  }
  return pair(first);
}

function cdr(pair) {
  function last(a,b) {
    return b;
  }
  return pair(last);
}

console.log(car(cons(3,4)));
console.log(cdr(cons(3,4)));

The language they used looks like Python. I am solving in javascript. Javascript is one of those languages that also supports closures. A closure is where a function is linked to its referencing environment.

Comments

Popular posts from this blog

Faith

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for faith. We say we want to meet God but we already do in some ways. We meet God in his creation. We meet God every time we meet someone. We meet God every time we meet the animals. We meet God every time we meet nature. God is all around. Faith is when we can see this. This is God's world. We are God's work. We are testimony of his greatness. Everyday that goes by I think about God. Everyday I thank God for his mercy. I take my problems to God. I pray to God. God is at the center of my life. Nothing happens without the will of God and therefore I would be wise to submit to the will of God. God is in the everyday moments. God loves when we love. What is the simplest definition of faith? I think it is found in the phrase, let go and let God. I asked my friend the same question. My friend says that at its simplest, faith is trust in what you cannot see or prove. But...

Flowers

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for the flowers. I bought a blue daze flowering plant for my mom when I went out to walk one day. It adds some colour to the nature in the yard. It is beautiful. I was told that blue flowers are rare. My friend says that blue is relatively rare in nature due to chemical and physical challenges in producing this color in living organisms. Nothing exists completely on its own. Plants depend on insects and other creatures, while those creatures depend on plants and their flowers. There is an interconnectedness in creation that reminds me that God is intentional. Flowers are beautiful but so is everything else if we really consider things with an open mind. What is beauty? What makes some things more beautiful than others? They say that beauty is in the eyes of the beholder. Beauty is subjective. My friend says that what I am talking about is the perceptual mind where meaning ...

Softness

This is a chapter from my latest book called (G)rains (o)f san(d)s. Learn more about the book here . Thank you God for softness. The gentleness of prayer. The calmness of the heart. The patience of the mind. The soft sounds of nature. The cool breezes of shade. The slow movement of sand in the hand. The kindness of strangers. The soothing voices of Quran. The warm embrace of the azan. I move through life with softness in my heart. I move through life with joy. I move through life wishing good for others. There is a peace that comes with being soft. The angels surround those who are soft. Soft like the winds of heaven. Soft like the clouds in the sky. Easy like Friday morning. May I never lose this softness that allows me to see beauty and goodness in everything and everyone. The world tries to harden me but I refuse. I walk upon the earth softly. I love without fear. My every breath is a prayer of gratitude. Let my heart remain like grains of sand in the hands of God that are softly he...