remove file name if it is already in the list

103 views Asked by At

I'm trying to remove an item (file name) from a list, nothing I have done works, no remove functions from the documentation, nothing from this website.. here is my code:

#lang racket

(define file-collection '())

(for ([f (directory-list)] #:when (regexp-match? "\\.log$" f))  
  (set! file-collection (foldr cons (list f) file-collection)))

(define (check-file file-in) 
  (for/list ([i (file->lines file-in)]) 
  (cond ((equal? i "[robot warrior]") (remove file-in file-collection) ))))

(for ([i file-collection])(check-file i))

still, no matter what I try, file-collection remains the same and all files are included in the collection.. however, if i use displayln file-in, only the files without [robot warrior] are displayed, and that's perfect, thats what I need, but I need to store it somehow. I find it very odd/weird how it refuses to work.

1

There are 1 answers

5
soegaard On

Here is two ways to remove certain files from a list.

#lang racket

(define all-files '("file1" "file2" "file3" "file4"))

(for/list ([file all-files]
           #:unless (equal? file "file2"))
  file)

(filter (lambda (file) (not (equal? file "file2")))
        all-files)

The result is:

'("file1" "file3" "file4")
'("file1" "file3" "file4")

Note that both for/list and filter rather han removing elements from an existing list produces new lists with the same elements (and omitting "file2" in this case).