2015-04-05 24 views
0

我试图创建一个列表,其中包含类似矩阵列表的列的所有值。一个球拍:使用两种条件进行筛选(将列转为列表)

(define-struct guess (symbol number)) 

puzzle1的 列表应只包含数字和猜测数:

(list 
    (list 'a 'b 'b 'c) 
    (list 'a 2 1 4) 
    (list 'f 3 'g 'g) 
    (list 'f 'h 'i 'i)) 

的参数是矩阵和一个位置,以致:

(check-expect (used-in-column puzzle1 (make-posn 0 1)) (list 2 3)) 

(define-struct puzzle (size board constraints)) 

其中板部分是上面需要的拼图1例子

我可以创建一个只有数字的列表,但似乎无法更改代码,因此它将任何猜测数字添加到列表中

所以这里有一个猜测的例子,里面有我的输出代码为空

(check-expect (used-in-column puzzle1partial2 (make-posn 0 1)) (list 2)) 

凡拼图板是

(list 
    (list (make-guess 'a 2) 'b 'b 'c) 
    (list 'a 2 1 4) 
    (list 'f 3 'g 'g) 
    (list 'f 'h 'i 'i)) 

到目前为止,我的代码如下所示:

(define (used-in-column puz pos) 
    (local [(define (columns board pos) 
      (cond 
       [(empty? board) empty] 
       [else (cons (list-ref (first board) (posn-x pos)) 
          (columns (rest board) pos))]))] 
    (cond  
     [(empty? puz) empty?] 
     [else (quicksort (filter number? (columns (puzzle-board puz) pos)) <)]))) 

我试过使用if语句和其他变体,但它最终导致我的代码乱七八糟,并没有得到任何结果。

有人可以给我任何意见吗?

+0

我不能运行这个。你忘了添加struct posn。 – Sylwester 2015-04-05 20:50:29

回答

2

至于你受你可以用标准的unzip转:

(define (unzip lsts) 
    (apply map list lsts)) 

(unzip '((1 2) (a b))) ; ==> ((1 a) (2 b)) 

如果你只是想要一个行使用list-REF:

​​

我真的不明白你的代码应该做什么,我也不能运行它(缺乏我认为的问题),所以我把它留在这里。