Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4539308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:55:44+00:00 2026-05-21T14:55:44+00:00

Let us assume that I have a 2d-list (or array if you so wish)

  • 0

Let us assume that I have a 2d-list (or array if you so wish) which looks like this:

'( (I I I O)
   (I X I O)
   (I I I O))

And now let us assume that I’d like to find all the neighbours of X. In this case my function would return a list of 8 I:s. How would I go about to implement this function in a smart way? I’ve already implemented a function which looks like this:

(defun get-neighbours (board x y)
  (let ((output '() ))
    (push (get-if-possible board (1+ x) y) output)
    (push (get-if-possible board (1- x) y) output)
    (push (get-if-possible board x (1+ y)) output)
    (push (get-if-possible board x (1- y)) output)
    (push (get-if-possible board (1+ x) (1+ y)) output)
    (push (get-if-possible board (1- x) (1- y)) output)
    (push (get-if-possible board (1+ x) (1- y)) output)
    (push (get-if-possible board (1- x) (1+ y)) output)
  output))

And that is so… Ugly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:55:44+00:00Added an answer on May 21, 2026 at 2:55 pm

    First, you are still in imperative land.

    (defun get-neighbours (board x y)
      (let ((output '() ))
        (push (get-if-possible board (1+ x) y) output)
        (push (get-if-possible board (1- x) y) output)
        (push (get-if-possible board x (1+ y)) output)
        (push (get-if-possible board x (1- y)) output)
        (push (get-if-possible board (1+ x) (1+ y)) output)
        (push (get-if-possible board (1- x) (1- y)) output)
        (push (get-if-possible board (1+ x) (1- y)) output)
        (push (get-if-possible board (1- x) (1+ y)) output)
      output))
    

    You do: variable declaration, bind it to NIL, mutation of the variable, returning the variable.

    It is simply:

    (defun get-neighbours (board x y)
      (list (get-if-possible board (1+ x) y)
            (get-if-possible board (1- x) y)
            (get-if-possible board x      (1+ y))
            (get-if-possible board x      (1- y))
            (get-if-possible board (1+ x) (1+ y))
            (get-if-possible board (1- x) (1- y))
            (get-if-possible board (1+ x) (1- y))
            (get-if-possible board (1- x) (1+ y))))
    

    You can ‘shorten’ the code with a local macro:

    (defun get-neighbours (board x y)
      (macrolet ((all-possible (&rest x-y-combinations)
                   `(list
                     ,@(loop for (a b) on x-y-combinations by #'cddr
                             collect `(get-if-posssible board ,a ,b)))))
        (all-possible (1+ x) y
                      (1- x) y
                      x      (1+ y)
                      x      (1- y)
                      (1+ x) (1+ y)
                      (1- x) (1- y)
                      (1+ x) (1- y)
                      (1- x) (1+ y))))
    

    Now one could abstract it a bit:

    (defmacro invoke-fn-on (fn &rest x-y-combinations)
      `(funcall (function ,fn)
                ,@(loop for (a b) on x-y-combinations by #'cddr
                        collect `(get-if-posssible board ,a ,b))))
    
    (defun get-neighbours (board x y)
      (invoke-fn-on list
                    (1+ x) y
                    (1- x) y
                    x      (1+ y)
                    x      (1- y)
                    (1+ x) (1+ y)
                    (1- x) (1- y)
                    (1+ x) (1- y)
                    (1- x) (1+ y)))
    

    About the LOOP:

    > (loop for (a b) on '(1 2 3 4 5 6) by #'cddr collect (list a b))
    ((1 2) (3 4) (5 6))
    

    ON moves the pattern (a b) over the list (1 2 3 4 5 6). It would give (1 2), (2 3), (3 4), (4 5) and (5 6). With each iteration the list is moved one forward by using CDR to get the rest list. BY now says that we move by two items, by CDDR and not one item as with CDR. So we get three iterations and the pairs (1 2), (3 4) and (5 6).

    An alternative would be to slightly simplify the LOOP by introducing a different list structure for the coordinate pairs:

    (defmacro invoke-fn-on (fn x-y-combinations)
      `(funcall (function ,fn)
                ,@(loop for (a b) in x-y-combinations
                        collect `(get-if-posssible board ,a ,b))))
    
    (defun get-neighbours (board x y)
      (invoke-fn-on list
                    '(((1+ x) y     )
                      ((1- x) y     )
                      (x      (1+ y))
                      (x      (1- y))
                      ((1+ x) (1+ y))
                      ((1- x) (1- y))
                      ((1+ x) (1- y))
                      ((1- x) (1+ y)))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class called Tuple. which looks like this : class tuple
Let's assume I have a model called product. Let's assume that product has three
Firstly, let me set out what I'd like to do. Assume I have three
Let's assume i got a list of ImageView and TextView with LinearLayout. Now i'm
let us assume that I have a reusable business layer that further makes use
Let's assume that some developer in my team shelved his changes that he did
Let's for a moment assume that you're programming in a language that uses camelcase
Let's assume, we create a stored procedure that is supposed to retrieve customer details
Or vice versa. Update: Hmm, let's assume I have a shopping cart app, the
This is one of those little detail (and possibly religious) questions. Let's assume we're

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.