Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. indirect enum IntList {
  2. case empty
  3. case cons(Int, IntList)
  4. }
  5.  
  6. extension IntList {
  7. static func append(_ xs: IntList, _ ys: IntList) -> IntList{
  8. switch xs {
  9. case .empty: return ys;
  10. case let .cons(head, tail): return .cons(head, append(tail, ys));
  11. }
  12. }
  13. func appending(_ xs: IntList) -> IntList {
  14. return IntList.append(self, xs);
  15. }
  16. }
  17.  
  18. func print(list: IntList) {
  19. func formatter(_ list: IntList) -> String {
  20. switch list {
  21. case .empty:
  22. return ""
  23. case let .cons(head, tail):
  24. let separator: String
  25. if case .empty = tail {
  26. separator = ""
  27. } else {
  28. separator = ", "
  29. }
  30. return String(head) + separator + formatter(tail)
  31. }
  32. }
  33.  
  34. print("[" + formatter(list) + "]");
  35. }
  36.  
  37. let xList: IntList = .cons(2, .cons(1, .empty))
  38. let yList: IntList = .cons(4, .cons(5, .empty))
  39. let zList = xList.appending(yList)
  40.  
  41. print(list: xList);
  42. print(list: yList);
  43. print(list: zList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement