Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- indirect enum IntList {
- case empty
- case cons(Int, IntList)
- }
- extension IntList {
- static func append(_ xs: IntList, _ ys: IntList) -> IntList{
- switch xs {
- case .empty: return ys;
- case let .cons(head, tail): return .cons(head, append(tail, ys));
- }
- }
- func appending(_ xs: IntList) -> IntList {
- return IntList.append(self, xs);
- }
- }
- func print(list: IntList) {
- func formatter(_ list: IntList) -> String {
- switch list {
- case .empty:
- return ""
- case let .cons(head, tail):
- let separator: String
- if case .empty = tail {
- separator = ""
- } else {
- separator = ", "
- }
- return String(head) + separator + formatter(tail)
- }
- }
- print("[" + formatter(list) + "]");
- }
- let xList: IntList = .cons(2, .cons(1, .empty))
- let yList: IntList = .cons(4, .cons(5, .empty))
- let zList = xList.appending(yList)
- print(list: xList);
- print(list: yList);
- print(list: zList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement