[英]Why list is not appending in Racket
我有一个小的文本文件,如下所示:
one, 50, 40, 65, 500
two, 80, 70, 100, 250
three, 100, 55, 125, 100
four, 50, 45, 58, 850
我正在尝试阅读它,并在每行第二列中列出所有值。 以下是我正在使用的代码:
#lang racket
(define (testfn fname)
(let ((sl '() ) (list2 (list)) (templist '()) (ss "") )
(set! sl (file->lines fname))
(for ((line sl))
(set! templist (string-split line ","))
(println templist)
(set! ss (list-ref templist 1))
(println ss)
(append list2 ss) ; does not work
(append list2 (list ss)) ; does not work
(cons ss list2) ; does not work
(cons (list ss) list2) ; does not work
(cons list2 (list ss)) ; does not work
(cons list2 ss) ; does not work
(println list2)
)
(println list2)))
(testfn "test.txt")
但是,在上面使用的许多方法中的任何一个方法中,“ list2”都没有附加字符串“ ss”。 输出显示:
'("one" " 50" " 40" " 65" " 500")
" 50"
'()
'("two" " 80" " 70" " 100" " 250")
" 80"
'()
'("three" " 100" " 55" " 125" " 100")
" 100"
'()
'("four" " 50" " 45" " 58" " 850")
" 50"
'()
'()
>
问题出在哪里,我该如何解决?
编辑:更正@JohnClements指出的错误后,以下代码有效:
#lang racket
(define (testfn fname)
(let ((sl '() ) (list2 (list)) (templist '()) (ss "") )
(set! sl (file->lines fname))
(for ((line sl))
(set! templist (string-split line ","))
(set! ss (list-ref templist 1))
(set! list2 (append list2 (list ss)))
(println list2)
)
(println list2)))
(testfn "test.txt")
输出:
'(" 50")
'(" 50" " 80")
'(" 50" " 80" " 100")
'(" 50" " 80" " 100" " 50")
'(" 50" " 80" " 100" " 50")
>
kes! 您的代码以非常必要的风格编写。 这种风格的代码很难阅读和维护。 我认为您会发现,如果将代码分解为更小的函数,并根据“如何设计程序”设计秘诀(www.htdp.org)开发代码,您将得到更整洁的东西。
您遇到的基本问题之一是假设诸如“附加”之类的功能会导致突变。 具体来说,您假设例如,如果您调用(append ab)
,则这些列表中的一个或两个在调用后将是不同的。 不是这种情况。
看看为什么,想象一下我写了这段代码:
#lang racket
(define a 3)
(define b 6)
(+ a b)
(- b a)
(+ (* 2 a) b)
运行此代码后, a
和b
的值是什么?
我认为您可能希望它们仍然是3和6。这是因为加法和减法不会改变他们的论点。 cons
和append
也是如此。 因此,调用(append ab)
会产生一个新列表,但是如果您不使用该值,那么它将不会随处可见。
在这里,让我很快为您编写一些代码...
编辑:
这是一个使用HtDP样式返回每个列表的第二个元素的程序:
#lang racket
(require rackunit)
;; given a list of lists, return the second of each list:
;; list-of-lists -> list
(define (second-element-map lol)
(cond [(empty? lol) empty]
[else (cons (second (first lol))
(second-element-map (rest lol)))]))
;; let's test it:
(check-equal? (second-element-map '((a b c) (d e f g) (1 2 3)))
'(b e 2))
;; given a list of lines, split each one into commas
(define (split-each-line lines)
(cond [(empty? lines) empty]
[else (cons (string-split (first lines) ",")
(split-each-line (rest lines)))]))
;; let's test it:
(check-equal? (split-each-line '("a,34,2987" "hn th, th"))
'(("a" "34" "2987")
("hn th" " th")))
;; given a filename, return a list containing the second element of
;; each list
;; path-string -> list
(define (testfn fname)
(second-element-map (split-each-line (file->lines fname))))
(testfn "/tmp/abc.txt")
可以更短吗? 当然。 HtDP样式干净,可以保证正常工作。
...但是这是我编写此程序供个人使用的方式:
#lang racket
(define (testfn2 fname)
(for/list ([l (in-list (file->lines fname))])
(second (string-split l ","))))
(testfn2 "/tmp/abc.txt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.