【问题标题】:Is there a way to make this print without a list inside a list?有没有办法在列表中没有列表的情况下进行打印?
【发布时间】:2019-10-30 15:03:36
【问题描述】:

我正在用 dr Racket 编写一个方案程序,它采用代表矩阵的数字列表将列表中的项目设置为给定的数字。到目前为止,它适用于案例第 1 列第 1 列,并且知道将数字放在哪里,但任何其他案例都会列出列表。我试图创建一个函数来帮助,但仍然收到同样的错误。任何帮助将不胜感激。 我得到的错误:

(setCell 矩阵 2 2 9) ((2 4 6 8) (1 (9 5 7)) (2 9 0 1))

我需要

(setCell 矩阵 2 2 9) ((2 4 6 8) (1 9 5 7) (2 9 0 1))

任何帮助将不胜感激。

(define Matrix '(( 2 4 6 8 )( 1 3 5 7)( 2 9 0 1)))

;getCell Matrix Row Column
;if i want row 2 col 2
(define (getCell Matrix Row Column)
  (if (= Row 1)
      (if (= Column 1)
          (car (car Matrix))
          (getCell (cons (cdr (car Matrix)) ()) Row (- Column 1))
       )
      (getCell (cdr Matrix) (- Row 1) Column)
  )
)
;> (getCell Matrix 1 1)
  ;2

;(define Matrix '(( 2 4 6 8 )( 1 3 5 7)( 2 9 0 1)))

;setCell Matrix Row Column Item
(define (setCell Matrix Row Column Item)
    (if (= Row 1)
          (if (= Column 1)
          (helpMe Matrix Item)
          (cons
           (cons (car (car Matrix))
                      (setCell (cons (cdr (car Matrix)) ()) Row (- Column 1) Item))
           (cdr Matrix))
       )
      (cons (car Matrix) (setCell (cdr Matrix) (- Row 1) Column Item))
  )
 )

 (define (helpMe Matrix Item)
      (cons (cons Item (cdr (car Matrix))) (cdr Matrix)))
  ;ERROR:
 ;>(setCell Matrix 2 2 9)
 ;((2 4 6 8) (1 (9 5 7)) (2 9 0 1))

 ;> (setCell Matrix 1 1 9)
 ;((9 4 6 8) (1 3 5 7) (2 9 0 1))

【问题讨论】:

    标签: scheme lisp racket


    【解决方案1】:

    这是一个常见问题。 基本思想是坐标,数据结构的形状,做一个很好的抽象,查看所有元素,给定坐标得到对应的值。

    这里我们定义左上元素是(1,1)(所以我们要减1)

    首先我们要建立一个相同的矩阵。其次,每个值由函数 f 确定。 f 输入是坐标 (i,j) 所以 f 是函数调用升级函数。您可以设置任何规则。就像一个常见的问题是问你建立对角矩阵,规则是 i=j。 (这是一个美丽的抽象)

    这意味着我们必须建立这个坐标

    (0,0) (0,1) (0,2) ...                        (0,(length (first m))
    (1,0) (1,1) (1,2)    ...                     (1,(length (first m))
    (2,0 ...                                                          
    ...
    (length of matrix),0)   ... ((length of matrix),(length (first m)))
    

    然后我们将坐标发送给 f。所以我们可以让 f 在输入矩阵中返回原始值,但是当 i = 行和 j = 列时,我们返回新值(项目)。同样的想法,您可以构建向量或其他,而不仅仅是列表。同样的想法可以用来构建三角形圆形或其他东西,而不仅仅是矩形。

    #lang racket
    
    (define (setCell  m row column item)
      (local ((define index-i (- row 1))
              (define index-j (- column 1))
              (define (f i j)
                (if (and (= i index-i) (= j index-j))
                    item
                    (list-ref (list-ref m i) j))))
        (build-list (length m) (lambda (i) (build-list (length (first m)) (lambda (j) (f i j)))))))
    
    
    ;;; TEST
    (define k  
      '((1 2 3)
        (1 2 3)
        (1 2 3)))
    
    (setCell k 1 1 100)
    (setCell k 2 3 100)
    
    (define k2  
      '((1 2 3)
        (1 2 3)))
    
    (setCell k2 1 3 100)
    (setCell k2 2 3 100)
    

    【讨论】:

      【解决方案2】:

      我正在用 dr Racket 编写一个方案程序,它接受一个代表矩阵的数字列表,将列表中的项目设置为给定的数字。

      #lang racket
      
      (define matrix-id (build-list 4 (λ (x) (build-list 4 (λ (y) (if (= x y) 1 2))))))
      ;; => '((1 0 0 0) (0 1 0 0) (0 0 1 0) (0 0 0 1))
      
      ;; [X] Number Number X [List-of [Lis-of X]] -> [List-of [Lis-of X]]
      (define (set-mat row col item mat)
        (for/list ([l mat] [i (length mat)])
          (for/list ([e l] [j (length l)])
            (if (and (= i row) (= j col))
                item
                e))))
      
      (set-mat 1 1 'fef matrix-id)
      ;; => '((1 0 0 0) (0 fef 0 0) (0 0 1 0) (0 0 0 1))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-31
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多