How to resolve the algorithm Unix/ls step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Unix/ls step by step in the Common Lisp programming language

Table of Contents

Problem Statement

Write a program that will list everything in the current folder,   similar to:

The output must be sorted, but printing extended details and producing multi-column output is not required.

For the list of paths:

When the program is executed in   /foo,   it should print: and when the program is executed in   /foo/bar,   it should print:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Unix/ls step by step in the Common Lisp programming language

Source code in the common programming language

(defun files-list (&optional (path "."))
  (let* ((dir (concatenate 'string path "/"))
         (abs-path (car (directory dir)))
         (file-pattern (concatenate 'string dir "*"))
         (subdir-pattern (concatenate 'string file-pattern "/")))
    (remove-duplicates
       (mapcar (lambda (p) (enough-namestring p abs-path))
               (mapcan #'directory (list file-pattern subdir-pattern)))
       :test #'string-equal)))

(defun ls (&optional (path "."))
  (format t "~{~a~%~}" (sort (files-list path) #'string-lessp)))


  

You may also check:How to resolve the algorithm Archimedean spiral step by step in the Ruby programming language
You may also check:How to resolve the algorithm System time step by step in the AWK programming language
You may also check:How to resolve the algorithm Symmetric difference step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Walk a directory/Recursively step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the Clojure programming language