problem.l 658 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. (def item (fn lst idx (do
  2. (def i 0)
  3. (while (gt idx i) (do
  4. (def lst (cdr lst))
  5. (def i (+ i 1))))
  6. (car lst)
  7. )))
  8. (def reverse (fn lst (do
  9. (def res (list))
  10. (while (car lst) (do
  11. (def res (cons (car lst) res))
  12. (def lst (cdr lst))
  13. ))
  14. res
  15. )))
  16. (def replace-item (fn lst pos new-value (do
  17. (def res (list))
  18. (def i 0)
  19. (while (car lst) (do
  20. (if (= pos i)
  21. (def res (cons new-value res))
  22. (def res (cons (car lst) res)))
  23. (def lst (cdr lst))
  24. (def i (+ i 1))
  25. ))
  26. (reverse res)
  27. )))
  28. (def mylist (list 1 2 3 4))
  29. (replace-item mylist 0 23)
  30. (replace-item mylist 1 (cons 1 2))
  31. (replace-item mylist 2 "foo")
  32. mylist