lists.scm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. (define-module (lang elisp primitives lists)
  2. #:use-module (lang elisp internals fset)
  3. #:use-module (lang elisp internals null)
  4. #:use-module (lang elisp internals signal))
  5. (fset 'cons cons)
  6. (fset 'null null)
  7. (fset 'not null)
  8. (fset 'car
  9. (lambda (l)
  10. (if (null l)
  11. %nil
  12. (car l))))
  13. (fset 'cdr
  14. (lambda (l)
  15. (if (null l)
  16. %nil
  17. (cdr l))))
  18. (fset 'eq
  19. (lambda (x y)
  20. (or (eq? x y)
  21. (and (null x) (null y)))))
  22. (fset 'equal
  23. (lambda (x y)
  24. (or (equal? x y)
  25. (and (null x) (null y)))))
  26. (fset 'setcar set-car!)
  27. (fset 'setcdr set-cdr!)
  28. (for-each (lambda (sym proc)
  29. (fset sym
  30. (lambda (elt list)
  31. (if (null list)
  32. %nil
  33. (if (null elt)
  34. (let loop ((l list))
  35. (cond ((null l) %nil)
  36. ((null (car l)) l)
  37. (else (loop (cdr l)))))
  38. (proc elt list))))))
  39. '( memq member assq assoc)
  40. `(,memq ,member ,assq ,assoc))
  41. (fset 'length
  42. (lambda (x)
  43. (cond ((null x) 0)
  44. ((pair? x) (length x))
  45. ((vector? x) (vector-length x))
  46. ((string? x) (string-length x))
  47. (else (wta 'sequencep x 1)))))
  48. (fset 'copy-sequence
  49. (lambda (x)
  50. (cond ((list? x) (list-copy x))
  51. ((vector? x) (error "Vector copy not yet implemented"))
  52. ((string? x) (string-copy x))
  53. (else (wta 'sequencep x 1)))))
  54. (fset 'elt
  55. (lambda (obj i)
  56. (cond ((pair? obj) (list-ref obj i))
  57. ((vector? obj) (vector-ref obj i))
  58. ((string? obj) (char->integer (string-ref obj i))))))
  59. (fset 'list list)
  60. (fset 'mapcar
  61. (lambda (function sequence)
  62. (map (lambda (elt)
  63. (elisp-apply function (list elt)))
  64. (cond ((null sequence) '())
  65. ((list? sequence) sequence)
  66. ((vector? sequence) (vector->list sequence))
  67. ((string? sequence) (map char->integer (string->list sequence)))
  68. (else (wta 'sequencep sequence 2))))))
  69. (fset 'nth
  70. (lambda (n list)
  71. (if (or (null list)
  72. (>= n (length list)))
  73. %nil
  74. (list-ref list n))))
  75. (fset 'listp
  76. (lambda (object)
  77. (or (null object)
  78. (list? object))))
  79. (fset 'consp pair?)
  80. (fset 'nconc
  81. (lambda args
  82. (apply append! (map (lambda (arg)
  83. (if arg arg '()))
  84. args))))