有没有办法在列表中没有列表的情况下进行打印?
Is there a way to make this print without a list inside a list?
我正在 dr racket 中编写一个方案程序,它采用表示矩阵的数字列表,将列表中的项目设置为给定的数字。到目前为止,它适用于第 1 行第 1 列的情况,并且知道在哪里放置数字,但在任何其他情况下它都会列出列表。我试图创建一个函数来提供帮助,但仍然收到相同的错误。任何帮助将不胜感激。
我得到的错误:
(setCell Matrix 2 2 9)
((2 4 6 8) (1 (9 5 7)) (2 9 0 1))
我需要
(setCell Matrix 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))
I am writing a scheme program in dr racket that takes a list of numbers representing a matrix sets an item in the list to the number given.
#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))
这是一个常见问题。
基本思想是坐标,数据结构形状,做好抽象,访问所有元素,给定坐标得到对应的值。
这里我们定义左上角的元素是(1,1)(所以我们要减1)
首先我们要构建一个相同的矩阵。其次每个值由函数 f 确定。而f的输入是坐标(i,j)所以f是一个函数调用upgrade函数。您可以设置任何规则。就像一个常见的问题是问你建立对角矩阵规则将是 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 return 输入矩阵中的原始值但是当 i = 行和 j = 列时我们 return 新值(项目)。同样的想法,你可以构建矢量或其他,而不仅仅是列表。同样的想法可以用来构建三角形圆或其他东西而不仅仅是矩形。
#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)
我正在 dr racket 中编写一个方案程序,它采用表示矩阵的数字列表,将列表中的项目设置为给定的数字。到目前为止,它适用于第 1 行第 1 列的情况,并且知道在哪里放置数字,但在任何其他情况下它都会列出列表。我试图创建一个函数来提供帮助,但仍然收到相同的错误。任何帮助将不胜感激。 我得到的错误:
(setCell Matrix 2 2 9) ((2 4 6 8) (1 (9 5 7)) (2 9 0 1))
我需要
(setCell Matrix 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))
I am writing a scheme program in dr racket that takes a list of numbers representing a matrix sets an item in the list to the number given.
#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))
这是一个常见问题。 基本思想是坐标,数据结构形状,做好抽象,访问所有元素,给定坐标得到对应的值。
这里我们定义左上角的元素是(1,1)(所以我们要减1)
首先我们要构建一个相同的矩阵。其次每个值由函数 f 确定。而f的输入是坐标(i,j)所以f是一个函数调用upgrade函数。您可以设置任何规则。就像一个常见的问题是问你建立对角矩阵规则将是 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 return 输入矩阵中的原始值但是当 i = 行和 j = 列时我们 return 新值(项目)。同样的想法,你可以构建矢量或其他,而不仅仅是列表。同样的想法可以用来构建三角形圆或其他东西而不仅仅是矩形。
#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)