Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                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`.
              
            
!

CSS

              
                /* The given question assumes (requires?) python -- I honestly don't know what other languages include a Pair primitive. Since I'm focusing on JS and this is on codepen... I'm going to adjust this a bit to JS where cons constructs an object, car needs to get the key, and cdr needs to get the value. */

/*  This seemed easy so I'm assuming the difficulty with this is something specific to pairs in Python?  Was thinking I could use an array of two entries instead of an object, but my understanding of a pair is associating the two elements and in JS we do that with an object... and this wouldn't be any harder with an array.*/
              
            
!

JS

              
                function cons(a,b){
  var pair = {};
  pair[a] = b;
  return pair;
}

function car( pair ){
  return Object.keys(pair)[0];
}

function cdr( pair ){
  return Object.values(pair)[0];
}

console.log(car(cons(3,4)))
console.log(cdr(cons(3,4)))
              
            
!
999px

Console