Search the Community
Showing results for tags 'odd'.
-
Hello, I would apprecite some reactions on the following tools that I made. Reading about Clojure I came across functionality that is not standard available in AutoLisp. After some thinking I programmed it myself. First 2 functions which do what they are supposed to do: (defun even? (n) (and (= (type n) 'INT) (= (rem n 2) 0))) (defun odd? (n) (and (= (type n) 'INT) (/= (rem n 2) 0))) The questionmark in the functionname seems appropriate. So what to do when you want to filter a list of integers against one of these functions? I constructed the following: (defun filter (f l / a) (foreach i l (if (apply f (list i)) (setq a (append a (list i))))) a) To get you started use the following two commands: (defun c:etest () (filter 'even? (list -5 -4 -3 -2 -1 0 1 2 3 4 5))) (defun c:otest () (filter 'odd? (list -5 -4 -3 -2 -1 0 1 2 3 4 5))) Although short already I wonder if the filter routine could be made more elegant/more logical. One can extend the library with all kinds of name? functions, provided that return T or nil on every call. The questionmark and the name 'filter' are used for logic readability. Try to make a functon that returns T if a value is of type 'STR, otherwise nil. Next use it with the filter function. It's fun and would like to see the variations that you come up with. Regards, André