-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path005.swift
More file actions
33 lines (28 loc) · 798 Bytes
/
005.swift
File metadata and controls
33 lines (28 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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):
// return lambda f: f(a, b)
//
// Implement car and cdr.
//
// author: Michal Klement, miklement@icloud.com
//
func cons(_ a: Int, _ b: Int) -> ((Int, Int) -> Int) -> Int {
func lambda(_ f: (Int, Int) -> Int) -> Int {
return f(a, b)
}
return lambda
}
func car(_ f: ((Int, Int) -> Int) -> Int) -> Int {
return f { a, _ in return a }
}
func cdr(_ f: ((Int, Int) -> Int) -> Int) -> Int {
return f { return $1 }
}
assert( car(cons(3, 4)) == 3 )
assert( cdr(cons(3, 4)) == 4 )