0
私はSchemeでリストをフォーマットし、各結果の前に行番号でフォーマットされたリストを印刷しようとしています。 ( " - " これは2つのサブリストを持つ大きな "S" リストで、内部の "W")Schemeに行番号を含むリストを印刷する方法は?
(define tree '
("S"
;;;inside S node
(
("-" ("A" 3333) ("A" 4444))
;;;inside W node
("W"
(
("+" ("R" 0) ("R" 1))
("+" ("R" 1) ("R" 2))
("+" ("R" 2) ("R" 3))
("+" ("R" 3) ("R" 4))
("+" ("R" 4) ("R" 5)));;;end of adds
(("-" ("R" 0) ("R" 1))
("-" ("R" 1) ("R" 2))
("-" ("R" 2) ("R" 3))
("-" ("R" 3) ("R" 4))
("-" ("A" 1000) ("A" 2000)));;;end of subs
);;;end of W node
);;;end of S node
)
)
:
リストを考えます。 そしてここでは私のコードです:(define (visit a)
(bigDumpNode a)
)
;;;open the big S list
(define (bigDumpNode a)
(cond
((eq? (car a) "S")
(bigSDumpList (cadr a))
)
)
)
;;;Loop the inner list
;;;There will be "-" list and "W" list
(define (bigSDumpList a)
(for-each (lambda (x)
(intoSmall x)
)a)
)
(define (intoSmall a)
;;;inside W list, we do have more smaller lists
(cond
((eq? (car a) "W")
(dumpWhile (cdr a))
)
((or
(eq? (car a) "+")
(eq? (car a) "-")
(eq? (car a) "*")
(eq? (car a) "/"))
(dumpTwoOperand a)
)
)
)
(define (dumpWhile a)
(dumpWhileLoop a)
)
;;;loop through the inner W list
(define (dumpWhileLoop a)
(for-each (lambda (x)
(operandLoop x)
(display "\nbne $00000000")
)a)
)
(define (operandLoop a)
(for-each (lambda (x)
(dumpTwoOperand x)
)a)
)
;;;format the + - */and print add sub mul div console
(define (dumpTwoOperand a)
(cond
((eq? (car a) "+")
(display "\nadd")
(twoOP(cdr a))
)
((eq? (car a) "-")
(display "\nsub")
(twoOP(cdr a))
)
((eq? (car a) "*")
(display "\nmul")
(twoOP(cdr a))
)
((eq? (car a) "/")
(display "\ndiv")
(twoOP(cdr a))
)
)
)
;;;format A to $ and R is still R
(define (twoOP a)
(oprand1 a)
(oprand2 a)
)
(define (oprand1 a)
(cond
((eq? (caar a) "A")
(display " $")
(display (cadar a))
)
((eq? (caar a) "R")
(display " R")
(display (cadar a))
)
)
)
(define (oprand2 a)
(cond
((eq? (caadr a) "A")
(display " $")
(display (cadadr a))
)
((eq? (caadr a) "R")
(display " R")
(display (cadadr a))
)
)
)
私はコンソールで(ツリーを訪問)を入力すると、それは以下の出力になります。
sub $3333 $4444
add R0 R1
add R1 R2
add R2 R3
add R3 R4
add R4 R5
bne $00000000
sub R0 R1
sub R1 R2
sub R2 R3
sub R3 R4
sub $1000 $2000
bne $00000000
私が欲しいものをするために、各出力の前に行番号を追加することです例:
0: sub $3333,$4444
1: add R0,R1
2: add R1,R2
3: add R2,R3
4: add R3,R4
5: add R4,R5
6: bne $00000000
...
どうすればよいですか? ありがとうございます:D
おかげでたくさん!それはとても甘いですね! –
@zuolizhuよろしくお願いします!あなたが私の答えに満足しているなら、私に感謝する良い方法は、それを受け入れたものとしてマークすることです。 –