Get a column as a list

Discussion of Common Lisp
Post Reply
MrCode
Posts: 3
Joined: Sat Mar 13, 2010 8:29 am

Get a column as a list

Post by MrCode » Sat Mar 13, 2010 8:49 am

Hi,

I'm trying to implement a simple sudoku solver and came up with this problem:

Consider this:

Code: Select all

(defvar board '(
(1 0 0 0 0 7 4 0 0)
(9 0 6 4 0 0 0 0 0)
(0 5 0 8 3 0 0 0 0)
(3 6 0 0 0 0 0 0 5)
(0 0 0 0 2 0 0 0 0)
(5 0 8 0 0 0 0 1 4)
(0 0 0 0 7 3 0 9 0)
(0 0 0 0 0 8 7 0 6)
(0 0 5 9 0 0 0 0 1)))
How can I get a list with the contents of the nth column?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Get a column as a list

Post by ramarren » Sat Mar 13, 2010 9:41 am

It would be better to represent the board as an array, since random access to contents is required. Then you can just manipulate the contents with indexes. Or perhaps using something like grid structured data library.

MrCode
Posts: 3
Joined: Sat Mar 13, 2010 8:29 am

Re: Get a column as a list

Post by MrCode » Sat Mar 13, 2010 12:18 pm

Thanks for the reply.

I'll give it a try.

JamesF
Posts: 98
Joined: Thu Jul 10, 2008 7:14 pm

Re: Get a column as a list

Post by JamesF » Tue Mar 16, 2010 8:06 pm

MrCode wrote:How can I get a list with the contents of the nth column?
With a cunning combination of mapcar and nth :)

Jasper
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands
Contact:

Re: Get a column as a list

Post by Jasper » Fri Mar 19, 2010 2:46 pm

To pack a question in an answer:

Code: Select all

(defun curry-l (fun &rest curried)
  "Curry to the left; add arguments to the start of the function.
Note: uses apply.. Hope it will optimize."
  (lambda (&rest args)
    (apply fun (append curried args))))

(defun nth-column (table-list n)
  (mapcar (curry-l #'nth n) table-list))
Does that curry optimize? Why isn't it in CL?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Get a column as a list

Post by gugamilare » Fri Mar 19, 2010 9:03 pm

Jasper wrote:To pack a question in an answer:

Code: Select all

(defun curry-l (fun &rest curried)
  "Curry to the left; add arguments to the start of the function.
Note: uses apply.. Hope it will optimize."
  (lambda (&rest args)
    (apply fun (append curried args))))

(defun nth-column (table-list n)
  (mapcar (curry-l #'nth n) table-list))
Does that curry optimize? Why isn't it in CL?
(1) No, it does not optimize, and (2) who knows? I agree with you, it is so pretty, it should be in the standard ;)

Curry can be optimized if a proper compiler macro is written. For instance, see alexandria's code, it has compiler macros for curry, rcurry, compose and others.

MrCode
Posts: 3
Joined: Sat Mar 13, 2010 8:29 am

Re: Get a column as a list

Post by MrCode » Sat May 01, 2010 9:11 am

Thanks, solved! Now I came up with another problem. Consider this function:

Code: Select all

(defun list-intersection (l1 l2)
  (cond
     ((null l1) nil)
     ((member (first l1) l2)  (cons (first l1) (list-intersection (rest l1) l2)))
     (t (list-intersection (rest l1) l2))))
It works fine with: (A B C) and (H F A) -> (A), but I wanted it to work with: ((A B C) (D F)) and ((A J K) (D F)) -> nil.

Post Reply