Remove file name if it is already in the list

I am trying to remove an item (filename) from the list, did nothing, remove features from the documentation, nothing from this site ... 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))

      

yet, no matter what I try, the collection of files remains the same and all files are included in the collection. However, if I use displayln file, only files without [robot warrior] are displayed, which is fine, this is what I need, but I need to store it somehow. I find it very strange / strange how it refuses to work.

+3


source to share


1 answer


Here are two ways to remove specific files from the 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)

      

Result:



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

      

Note that both for/list

, and filter

rather, removes items from the existing list, creates new lists with the same items (and in this case skips "file2").

+1


source







All Articles