New to LISP

Discussion of Common Lisp
Post Reply
davidlever
Posts: 3
Joined: Fri Mar 11, 2016 8:30 am

New to LISP

Post by davidlever » Fri Mar 11, 2016 8:34 am

Hi all, I am very new to lisp. I have this function (adopted from vacuum agent problem)

(defun find-closest-in-grid (radar type pos-x pos-y)
(labels ((distance (x y)
(+ (abs (- x pos-x))
(abs (- y pos-y)))))
(destructuring-bind (width height)
(array-dimensions radar)
(let ((best nil)
((best-distance (+ width height))))
(loop for x from 0 below width
do (loop for y from 0 below height
do (loop for element in (aref radar x y)
do (when (eql (type-of element) type)
(when (<= (distance x y) best-distance)
(setf best (list x y))
(setf best-distance (distance x y))))))))
best)))


But i wish to convert to (defun find-closest-in-grid (radar type ) with only two parameters while keeping the remaining function the same. How should go about doing it

David Mullen
Posts: 78
Joined: Mon Dec 01, 2014 12:29 pm
Contact:

Re: New to LISP

Post by David Mullen » Sat Mar 12, 2016 11:33 am

You mean you want the position to be global variables instead of parameters?

davidlever
Posts: 3
Joined: Fri Mar 11, 2016 8:30 am

Re: New to LISP

Post by davidlever » Sat Mar 12, 2016 8:42 pm

Yes David. Thanks for the great help.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: New to LISP

Post by Goheeca » Mon Mar 14, 2016 6:12 am

Or you can take more functional approach and return a lambda function so instead of:

Code: Select all

(defun find-closest-in-grid (radar type pos-x pos-y)
  (labels ((distance (x y) 
             (+ (abs (- x pos-x)) 
                (abs (- y pos-y)))))
    (destructuring-bind (width height) (array-dimensions radar)
      (let ((best nil)
            ((best-distance (+ width height))))
        (loop for x from 0 below width
              do (loop for y from 0 below height
                       do (loop for element in (aref radar x y)
                                do (when (eql (type-of element) type)
                                     (when (<= (distance x y) best-distance)
                                       (setf best (list x y))
                                       (setf best-distance (distance x y)))))))
        best))))
you write:

Code: Select all

(defun get-find-closest-in-grid (pos-x pos-y)
  (lambda (radar type)
    (labels ((distance (x y) 
               (+ (abs (- x pos-x)) 
                  (abs (- y pos-y)))))
      (destructuring-bind (width height) (array-dimensions radar)
        (let ((best nil)
              ((best-distance (+ width height))))
          (loop for x from 0 below width
                do (loop for y from 0 below height
                         do (loop for element in (aref radar x y)
                                  do (when (eql (type-of element) type)
                                       (when (<= (distance x y) best-distance)
                                         (setf best (list x y))
                                         (setf best-distance (distance x y)))))))
          best)))))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Post Reply