Page 1 of 1
Get a column as a list
Posted: Sat Mar 13, 2010 8:49 am
by MrCode
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?
Re: Get a column as a list
Posted: Sat Mar 13, 2010 9:41 am
by ramarren
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.
Re: Get a column as a list
Posted: Sat Mar 13, 2010 12:18 pm
by MrCode
Thanks for the reply.
I'll give it a try.
Re: Get a column as a list
Posted: Tue Mar 16, 2010 8:06 pm
by JamesF
MrCode wrote:How can I get a list with the contents of the nth column?
With a cunning combination of mapcar and nth

Re: Get a column as a list
Posted: Fri Mar 19, 2010 2:46 pm
by Jasper
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?
Re: Get a column as a list
Posted: Fri Mar 19, 2010 9:03 pm
by gugamilare
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.
Re: Get a column as a list
Posted: Sat May 01, 2010 9:11 am
by MrCode
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.