lists.l 789 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (
  2. (def = (fn a b (if (- a b) 0 1)))
  3. (def not (fn a (if a 0 1)))
  4. (def and (fn a b (if a (if b 1 0) 0)))
  5. (def or (fn a b (if a 1 (if b 1 0) 0)))
  6. (def length (fn lst (do
  7. (def i 0)
  8. (while (cdr lst) (do
  9. (def i (+ i 1))
  10. (def lst (cdr lst))
  11. ))
  12. i
  13. )))
  14. (def reverse (fn lst (do
  15. (def res (list))
  16. (while (cdr lst) (do
  17. (def res (cons (car lst) res))
  18. (def lst (cdr lst))
  19. ))
  20. res
  21. )))
  22. (def item (fn lst idx (do
  23. (def i 0)
  24. (while (gt idx i) (do
  25. (def lst (cdr lst))
  26. (def i (+ i 1))))
  27. (car lst)
  28. )))
  29. (def replace-item (fn lst pos new-value ((fn res i (do
  30. (while (cdr lst) (do
  31. (if (= pos i)
  32. (def res (cons new-value res))
  33. (def res (cons (car lst) res)))
  34. (def lst (cdr lst))
  35. (def i (+ i 1))
  36. ))
  37. (reverse res)
  38. ))
  39. (list) 0)
  40. ))
  41. )