为什么列表不会附加在Racket中

时间:2016-07-13 23:07:05

标签: scheme racket

我有一个小文本文件如下:

one, 50, 40, 65, 500
two, 80, 70, 100, 250
three, 100, 55, 125, 100
four, 50, 45, 58, 850

我正在尝试阅读它并列出每行第2列中的所有值。以下是我正在使用的代码:

#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")
> 

1 个答案:

答案 0 :(得分:3)

糟糕!您的代码以非常强制的方式编写。这种风格的代码很难阅读和维护。我想你会发现,如果你将代码分解成更小的函数,并根据如何设计程序设计方法(www.htdp.org)开发代码,你就会得到更清洁的东西。

您遇到的一个基本问题是假设“追加”等功能会导致突变。具体来说,您假设如果您打电话,例如(append a b),那么这些列表中的一个或两个在通话后会有所不同。不是这种情况。

要知道为什么,想象一下我写了这段代码:

#lang racket

(define a 3)
(define b 6)
(+ a b)
(- b a)
(+ (* 2 a) b)

运行此代码后,ab的值会是多少?

我认为你可能期望它们仍然是3和6.那是因为加法 和减法不会改变他们的论点。 cons也是如此 和append。所以调用(append a b)会产生一个新的列表,但是如果你不使用那个值,那么它就不会去任何地方。

在这里,让我快速为你写一些代码......

编辑:

这是一个程序,它使用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")
相关问题