pairs.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 (caaar x) (car (car (car x))))
  55. (define (cadar x) (car (cdr (car x))))
  56. (define (caadr x) (car (car (cdr x))))
  57. (define (caddr x) (car (cdr (cdr x))))
  58. (define (cdaar x) (cdr (car (car x))))
  59. (define (cddar x) (cdr (cdr (car x))))
  60. (define (cdadr x) (cdr (car (cdr x))))
  61. (define (cdddr x) (cdr (cdr (cdr x))))
  62. (define (caaaar x) (car (car (car (car x)))))
  63. (define (caadar x) (car (car (cdr (car x)))))
  64. (define (caaadr x) (car (car (car (cdr x)))))
  65. (define (caaddr x) (car (car (cdr (cdr x)))))
  66. (define (cadaar x) (car (cdr (car (car x)))))
  67. (define (caddar x) (car (cdr (cdr (car x)))))
  68. (define (cadadr x) (car (cdr (car (cdr x)))))
  69. (define (cadddr x) (car (cdr (cdr (cdr x)))))
  70. (define (cdaaar x) (cdr (car (car (car x)))))
  71. (define (cdadar x) (cdr (car (cdr (car x)))))
  72. (define (cdaadr x) (cdr (car (car (cdr x)))))
  73. (define (cdaddr x) (cdr (car (cdr (cdr x)))))
  74. (define (cddaar x) (cdr (cdr (car (car x)))))
  75. (define (cdddar x) (cdr (cdr (cdr (car x)))))
  76. (define (cddadr x) (cdr (cdr (car (cdr x)))))
  77. (define (cddddr x) (cdr (cdr (cdr (cdr x))))))