0
\$\begingroup\$

Background info: An FSObject is a

dir or a file.

The structure definition for a directory (dir) is the following

(define-struct dir (name owner contents)).

A directory (dir) is a

(make-dir Str Sym (listof FSObject)).

The structure definition for a file is the following:

(define-struct file (name size owner)).

A file is a

(make-file Str Nat Sym).

The function I want to write is supposed to only delete a directory if its contents are empty and NOT any other directories that have empty contents as a result. If the function consumes a single directory and its contents are empty, then the function does nothing to that directory.

Here is the ridiculously complex code I wrote (I was wondering if someone could help me simplify it? I think it can be simplified greatly! Also, even if it's correct, it still can't be so complex!):

(define-struct file (name size owner))
(define-struct dir (name owner contents))

;; constants

(define example-fs
  (make-dir
   "root" 'root
   (list
    (make-dir
     "Dan" 'dan
     (list (make-file "log.txt" 768 'dan)
           (make-file "profile.jpg" 60370 'dan)
           (make-dir "music" 'dan
                     (list (make-file "Thelonius Monk.mp3" 92227584 'dan)))))
    (make-dir
     "Slides" 'teaching
     (list (make-dir
            "cs135" 'teaching
            (list (make-file "01-intro.pdf" 72244 'teaching)
                  (make-file "11-trees.pdf" 123124 'teaching)
                  (make-dir "system" 'root
                            (list (make-dir "logs" 'teaching empty)))))))
    (make-file "vmlinuz" 30 'root))))

(define example-fs2
  (make-dir
   "filesystem" 'Jordan
   (list
    (make-dir
     "file" 'Gom
     (list (make-file "interesting" 46 'funny)))
    (make-dir
     "snowboarding" 'snowboards
     (list (make-file "nonsense" 55 'nonsense)
           (make-file "toobig?" 100 'toobig)
           (make-file "lotsoffiles" 1000 'lotsoffiles)))
    (make-file "havesomefun" 50 'william))))

(define example-fs3
  (make-dir
   "file storage" 'steward
   (list (make-dir
          "important files" 'steward
          (list (make-dir
                 "interesting patterns" 'frank
                 (list (make-dir
                        "recursion" 'frank
                        empty)))))
         (make-dir
          "some notes" 'steward
          (list (make-dir
                 "stuff that needs to be done" 'steward
                 (list (make-file
                        "spend less time on cs assignments" 100 'steward)
                       (make-dir
                        "learn about arbitrary nesting" 'steward
                        empty)))))
         (make-dir
          "trees" 'steward
          (list (make-file "basics about trees in cs" 'steward empty))))))

;; function w/ encapsulated helper functions
(define (delete fsobject)
  (local
    [(define (delete1 fsobject count)
       (cond
         [(or (file? fsobject)
              (and (zero? count) (empty? (dir-contents fsobject)))) fsobject]
         [(empty? (dir-contents fsobject)) empty]
         [else
          (local
            [(define (delete/list lofso)
               (cond [(empty? lofso) empty]
                     [else
                      (local [(define nonempty_lofso
                                (delete1 (first lofso) 1))]
                        (cond [(file? nonempty_lofso)
                               (cons nonempty_lofso
                                     (delete/list (rest lofso)))]
                              [(empty? nonempty_lofso)
                               (delete/list (rest lofso))]
                              [else
                               (cons nonempty_lofso
                                     (delete/list (rest lofso)))]))]))]
            (make-dir (dir-name fsobject) (dir-owner fsobject)
             (delete/list (dir-contents fsobject))))]))]
    (delete1 fsobject 0)))

;; tests
(check-expect (delete (make-file "fso" 49 'fred))
              (make-file "fso" 49 'fred))
(check-expect (delete (make-dir "system" 'root empty))
              (make-dir "system" 'root empty))
(check-expect (delete (make-dir "important files" 'fred empty))
              (make-dir "important files" 'fred empty))
(check-expect (delete (make-dir
                             "important info" 'fred
                             (list (make-dir "exam prep" 'fred empty))))
              (make-dir "important info" 'fred empty))
(check-expect (delete
               (make-dir
                "computer programming" 'frank
                (list (make-dir
                       "introduction" 'fred
                       (list (make-dir
                              "intermediate level topics" 'fred empty))))))
              (make-dir
               "computer programming" 'frank
               (list (make-dir
                      "introduction" 'fred
                      empty))))
(check-expect (delete example-fs3)
              (make-dir
               "file storage"
               'steward
               (list (make-dir
                      "important files" 'steward
                      (list (make-dir
                             "interesting patterns" 'frank empty)))
                     (make-dir
                      "some notes" 'steward
                      (list (make-dir
                             "stuff that needs to be done" 'steward
                             (list (make-file
                                    "spend less time on cs assignments" 100
                                    'steward)))))
                     (make-dir
                      "trees" 'steward
                      (list (make-file
                             "basics about trees in cs" 'steward empty))))))
(check-expect (delete example-fs)
              (make-dir
               "root" 'root
               (list
                (make-dir
                 "Dan" 'dan
                 (list (make-file "log.txt" 768 'dan)
                       (make-file "profile.jpg" 60370 'dan)
                       (make-dir
                        "music" 'dan
                        (list
                         (make-file
                          "Thelonius Monk.mp3" 92227584 'dan)))))
                (make-dir
                 "Slides" 'teaching
                 (list (make-dir
                        "cs135" 'teaching
                        (list
                         (make-file
                          "01-intro.pdf" 72244 'teaching)
                         (make-file
                          "11-trees.pdf" 123124 'teaching)
                         (make-dir "system" 'root empty)))))
                (make-file "vmlinuz" 30 'root))))

Edit: I've changed the code for my function bc I want to encapsulate all the helper functions with local. However, this makes my code ridiculously complex.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.