code_junkie

Can I Do This In Lisp?

Nov 10th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. (defun my-index (findMe mylist)
  2. (let ((counter 0) (found 1))
  3. (dolist (item mylist)
  4. (cond
  5. ((eq item findMe) ;this works because 'eq' checks place in memory,
  6. ;and as long as 'findMe' was from the original list, this will work.
  7. (setq found nil)
  8. (found (incf counter))))
  9. counter))
  10.  
  11. (let ((myList '(1 2 3 4 5 6)))
  12. (setf (nth 4 myList) 101); <----
  13. myList)
  14.  
  15. (1 2 3 4 101 6)
  16.  
  17. (let ((myList '(1 2 3 4 5 6)))
  18. (setf (nth 4 myList) 101)
  19. (list myList (position 101 myList)))
  20.  
  21. ((1 2 3 4 101 6) 4)
  22.  
  23. > (setq numbers (list 1 2 3 4))
  24. (1 2 3 4)
  25. > (position 3 numbers)
  26. 2
  27.  
  28. (define list-position
  29. (lambda (o l)
  30. (let loop ((i 0) (l l))
  31. (if (null? l) #f
  32. (if (eqv? (car l) o) i
  33. (loop (+ i 1) (cdr l)))))))
  34.  
  35. ----------------------------------------------------
  36.  
  37. > (define numbers (list 1 2 3 4))
  38. > (list-position 3 numbers)
  39. 2
  40. >
  41.  
  42. (defun my-attr (list)
  43. (nth 4 list))
  44.  
  45. (defun (setf my-attr) (new list)
  46. (setf (nth 4 list) new))
Add Comment
Please, Sign In to add comment