Viewing a single comment thread. View all comments


bea wrote (edited )

Well... I spent the whole night learning basics of Lisp just for this.

I don't regret it though, it's a suprisingly nice language. Probably not for me to use, but I like it as a concept.

(defun my_nth (n list)
  (unless (and (typep n 'integer) (>= n 0))
    (error "~a is not a non-negative integer" n))
  (unless (typep list 'list)
    (error "~a is not a list" n))
  (let ((i 0))
    (loop for item in list do
      (if (= i n)
        (return item)
        (incf i)))))

I actually don't know i how fast it is since I have no idea how to measure time/speed in Common Lisp so you'll have to do that.

Btw, your function didn't work for me. If I put in a list as the second argument it complains on (> n list) that the list isn't a number and If it's a number lower than n it complains on (cdr nl) that it's not a list :)


[deleted] wrote (edited )


bea wrote

Umm... now you are returning the list from cdr. You should change the last nl to (car nl) to return a proper result.