pairs.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; Pairs
  2. ;;; Copyright (C) 2024 Igalia, S.L.
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Pairs, and null?.
  18. ;;;
  19. ;;; Code:
  20. (library (hoot pairs)
  21. (export null?
  22. pair?
  23. cons
  24. car
  25. cdr
  26. set-car!
  27. set-cdr!
  28. cons*
  29. list
  30. caar cdar cadr cddr
  31. caaar cadar caadr caddr cdaar cddar cdadr cdddr
  32. caaaar caadar caaadr caaddr cadaar caddar cadadr cadddr
  33. cdaaar cdadar cdaadr cdaddr cddaar cdddar cddadr cddddr)
  34. (import (hoot primitives))
  35. (define (null? x) (%null? x))
  36. (define (pair? p) (%pair? p))
  37. (define (cons x y) (%cons x y))
  38. (define (car x) (%car x))
  39. (define (cdr x) (%cdr x))
  40. (define (set-car! x y) (%set-car! x y))
  41. (define (set-cdr! x y) (%set-cdr! x y))
  42. (define (%generic-cons* head . tail)
  43. (if (null? tail)
  44. head
  45. (cons head (apply %generic-cons* tail))))
  46. (define-syntax cons*
  47. (lambda (stx)
  48. (syntax-case stx ()
  49. ((_) #'(%generic-cons*))
  50. ((_ a) #'a)
  51. ((_ a . b) #'(%cons a (cons* . b)))
  52. (f (identifier? #'f) #'%generic-cons*))))
  53. (define (list . args) args)
  54. (define (caar x) (car (car x)))
  55. (define (cadr x) (car (cdr x)))
  56. (define (cdar x) (cdr (car x)))
  57. (define (cddr x) (cdr (cdr x)))
  58. (define (caaar x) (car (car (car x))))
  59. (define (cadar x) (car (cdr (car x))))
  60. (define (caadr x) (car (car (cdr x))))
  61. (define (caddr x) (car (cdr (cdr x))))
  62. (define (cdaar x) (cdr (car (car x))))
  63. (define (cddar x) (cdr (cdr (car x))))
  64. (define (cdadr x) (cdr (car (cdr x))))
  65. (define (cdddr x) (cdr (cdr (cdr x))))
  66. (define (caaaar x) (car (car (car (car x)))))
  67. (define (caadar x) (car (car (cdr (car x)))))
  68. (define (caaadr x) (car (car (car (cdr x)))))
  69. (define (caaddr x) (car (car (cdr (cdr x)))))
  70. (define (cadaar x) (car (cdr (car (car x)))))
  71. (define (caddar x) (car (cdr (cdr (car x)))))
  72. (define (cadadr x) (car (cdr (car (cdr x)))))
  73. (define (cadddr x) (car (cdr (cdr (cdr x)))))
  74. (define (cdaaar x) (cdr (car (car (car x)))))
  75. (define (cdadar x) (cdr (car (cdr (car x)))))
  76. (define (cdaadr x) (cdr (car (car (cdr x)))))
  77. (define (cdaddr x) (cdr (car (cdr (cdr x)))))
  78. (define (cddaar x) (cdr (cdr (car (car x)))))
  79. (define (cdddar x) (cdr (cdr (cdr (car x)))))
  80. (define (cddadr x) (cdr (cdr (car (cdr x)))))
  81. (define (cddddr x) (cdr (cdr (cdr (cdr x))))))