Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 5
Good morning! Here's your coding interview problem for today.

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.

Pull request description:
Day5
Resolves #9 

Patch:
diff --git a/README.md b/README.md
index 338a3df..29f92b7 100644
--- a/README.md
+++ b/README.md
@@ -13,4 +13,5 @@ problems from
 * [Day 2](https://github.com/vaskoz/dailycodingproblem-go/issues/3)
 * [Day 3](https://github.com/vaskoz/dailycodingproblem-go/issues/7)
 * [Day 4](https://github.com/vaskoz/dailycodingproblem-go/issues/5)
+* [Day 5](https://github.com/vaskoz/dailycodingproblem-go/issues/9)
 
diff --git a/day5/problem.go b/day5/problem.go
new file mode 100644
index 0000000..d7394d5
--- /dev/null
+++ b/day5/problem.go
@@ -0,0 +1,29 @@
+package day5
+
+// PairOperator is a function that can operate on a pair and return a value.
+type PairOperator func(interface{}, interface{}) interface{}
+
+// PairFunc is a function that takes a PairOperator function as an argument and returns a value.
+type PairFunc func(PairOperator) interface{}
+
+// Cons is given by the problem.
+// Arguments a and b can be anything and returns a PairFunc.
+func Cons(a, b interface{}) PairFunc {
+	return func(f PairOperator) interface{} {
+		return f(a, b)
+	}
+}
+
+// Car invokes the PairFunc to operate on the pair and return the first value in the pair.
+func Car(pair PairFunc) interface{} {
+	return pair(func(a, b interface{}) interface{} {
+		return a
+	})
+}
+
+// Cdr invokes the PairFunc to operate on the pair and return the second value in the pair.
+func Cdr(pair PairFunc) interface{} {
+	return pair(func(a, b interface{}) interface{} {
+		return b
+	})
+}


Test patch:
diff --git a/day5/problem_test.go b/day5/problem_test.go
new file mode 100644
index 0000000..8dfc5d7
--- /dev/null
+++ b/day5/problem_test.go
@@ -0,0 +1,34 @@
+package day5
+
+import "testing"
+
+func TestCar(t *testing.T) {
+	t.Parallel()
+	result := Car(Cons(3, 4))
+	if result != 3 {
+		t.Errorf("Expected 3 but got %d", result)
+	}
+}
+
+func TestCdr(t *testing.T) {
+	t.Parallel()
+	result := Cdr(Cons(3, 4))
+	if result != 4 {
+		t.Errorf("Expected 4 but got %d", result)
+	}
+}
+
+// Pretty much just benchmarking function call performance.
+// Benchmarks just included for completeness, but not necessary.
+
+func BenchmarkCar(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		Car(Cons(3, 4))
+	}
+}
+
+func BenchmarkCdr(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		Cdr(Cons(3, 4))
+	}
+}