pairs.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (only (hoot primitives)
  35. %null? %pair? %cons %car %cdr %set-car! %set-cdr!)
  36. (hoot apply)
  37. (hoot syntax))
  38. (define (null? x) (%null? x))
  39. (define (pair? p) (%pair? p))
  40. (define (cons x y) (%cons x y))
  41. (define (car x) (%car x))
  42. (define (cdr x) (%cdr x))
  43. (define (set-car! x y) (%set-car! x y))
  44. (define (set-cdr! x y) (%set-cdr! x y))
  45. (define (%generic-cons* head . tail)
  46. (if (null? tail)
  47. head
  48. (cons head (apply %generic-cons* tail))))
  49. (define-syntax cons*
  50. (lambda (stx)
  51. (syntax-case stx ()
  52. ((_) #'(%generic-cons*))
  53. ((_ a) #'a)
  54. ((_ a . b) #'(%cons a (cons* . b)))
  55. (f (identifier? #'f) #'%generic-cons*))))
  56. (define (list . args) args)
  57. (define (caar x) (car (car x)))
  58. (define (cadr x) (car (cdr x)))
  59. (define (cdar x) (cdr (car x)))
  60. (define (cddr x) (cdr (cdr x)))
  61. (define (caaar x) (car (car (car x))))
  62. (define (cadar x) (car (cdr (car x))))
  63. (define (caadr x) (car (car (cdr x))))
  64. (define (caddr x) (car (cdr (cdr x))))
  65. (define (cdaar x) (cdr (car (car x))))
  66. (define (cddar x) (cdr (cdr (car x))))
  67. (define (cdadr x) (cdr (car (cdr x))))
  68. (define (cdddr x) (cdr (cdr (cdr x))))
  69. (define (caaaar x) (car (car (car (car x)))))
  70. (define (caadar x) (car (car (cdr (car x)))))
  71. (define (caaadr x) (car (car (car (cdr x)))))
  72. (define (caaddr x) (car (car (cdr (cdr x)))))
  73. (define (cadaar x) (car (cdr (car (car x)))))
  74. (define (caddar x) (car (cdr (cdr (car x)))))
  75. (define (cadadr x) (car (cdr (car (cdr x)))))
  76. (define (cadddr x) (car (cdr (cdr (cdr x)))))
  77. (define (cdaaar x) (cdr (car (car (car x)))))
  78. (define (cdadar x) (cdr (car (cdr (car x)))))
  79. (define (cdaadr x) (cdr (car (car (cdr x)))))
  80. (define (cdaddr x) (cdr (car (cdr (cdr x)))))
  81. (define (cddaar x) (cdr (cdr (car (car x)))))
  82. (define (cdddar x) (cdr (cdr (cdr (car x)))))
  83. (define (cddadr x) (cdr (cdr (car (cdr x)))))
  84. (define (cddddr x) (cdr (cdr (cdr (cdr x))))))