Tonight's Meeting
vj at marilyn.indstate.edu
vj at marilyn.indstate.edu
Wed Jun 4 19:02:24 EDT 2008
How about Haskell??
-- Begin --
import System.Directory
import Control.Monad(forM)
import System.FilePath ((</>))
tree dirname = tree' dirname ""
tree' dirname spacing = do
contents <- getDirectoryContents dirname
let propernames = filter (not . (`elem` [".", ".."])) contents
forM propernames $ \name -> do
let path = dirname </> name
putStrLn $ spacing ++ name
isDirectory <- doesDirectoryExist path
if isDirectory
then tree' path (spacing ++ " ")
else return ()
return ()
-- End --
On Jun 04, Brian Lakstins wrote:
> I wasn't really sure of the formatting that the tree should take from the
> example, so I formatted the output a little differently.
>
> Root Dir
> D- Dir 1a
> F-- File 1a
> F-- File 1b
> D- Dir 1b
> D-- Dir 2a
> F--- File 2a
>
> Here's the code in C#:
>
> using System;
> using System.IO;
>
> namespace Tree
> {
> class Program
> {
> static void Main(string[] args)
> {
> string lsRootDirectory = @"c:\users\brian";
> DirectoryInfo loDirectory = new DirectoryInfo(lsRootDirectory);
> Console.WriteLine(loDirectory.Name);
> PrintTree(lsRootDirectory, 1);
> }
>
> static void PrintTree(string lsRootDirectory, int lnLevel)
> {
> try
> {
> foreach (string lsDirectory in
> Directory.GetDirectories(lsRootDirectory))
> {
> DirectoryInfo loDirectory = new
> DirectoryInfo(lsDirectory);
> Console.WriteLine(String.Format("D{0} {1}",
> "".PadLeft(lnLevel,'-'), loDirectory.Name));
> foreach (string lsFile in
> Directory.GetFiles(lsDirectory))
> {
> FileInfo loFile = new FileInfo(lsFile);
> Console.WriteLine(String.Format("F{0} {1}",
> "".PadLeft(lnLevel + 1,'-'),loFile.Name));
> }
> PrintTree(lsDirectory, lnLevel + 1);
> }
> }
> catch (Exception loE)
> {
> //Probably being caused by access being denied.
> }
> }
> }
> }
>
>
> The executable is 5120 bytes. Recursive functions is something that I've
> heard F# is good at, so I'd be interested on how this would be done in F#.
>
> Brian
>
>
> On Mon, Jun 2, 2008 at 6:27 PM, Steve Baker <ice at mama.indstate.edu> wrote:
>
> > "Paul Beel" <paul.beel at gmail.com> wrote:
> > > As far as activities when we do not have a speaker. Maybe we could do
> > > some kind of programming challenge. This would need to be a very small
> > > program to write in your favorite language. The challenge would need
> > > to be decided on and then we could show our code at the next meeting.
> > > A lot of us don't have a lot of free time, so again, this would need
> > > to be a small program. This is a fun thing to do that will give us a
> > > chance to use whatever language we want and let others see other
> > > languages in action. I have heard of groups doing something like this
> > > and they call it a coding dojo.
> > > If you have any ideas on a small program we could write, please e-mail
> > > the list and let us know.
> >
> > Being lazy, I nominate the first coding dojo to be a directory tree
> > lister,
> > since I have already coded this numerous times already. ;-)
> >
> > Basically make a tree view of your filesystem, like so:
> >
> > dir1
> > |-- file
> > |-- dir2
> > | |-- a
> > | |-- b
> > | `-- c
> > `-- dir3
> > |-- x
> > |-- y
> > `-- z
> >
> > I think it would be interesting to see just how small such a program can
> > get. Here is tree in clam:
> >
> > ----
> > #!/root/clam/clam
> > #include "stdlib.h"
> > #include "stat.h"
> >
> > main(list argv)
> > {
> > int files = 0, dirs = 0;
> >
> > if (sizeof(argv) == 1) argv[1] = ".";
> >
> > for(i=1; i < sizeof(argv); i++) {
> > st = stat(argv[i]);
> > if ((st[ST_MODE] & S_IFMT) != S_IFDIR) {
> > print(argv[i], "\n");
> > continue;
> > }
> > print(argv[i],"\n");
> > [files, dirs] += tree(argv[i], "");
> > }
> > print("\n", dirs, " director", dirs==1?"y":"ies", ", ", files,
> > " file", files>1?"s":"", "\n");
> > }
> >
> > tree(str path, str indent)
> > {
> > int files = 0, dirs = 0;
> >
> > foreach([de, i, sz]; readdirectory(path)) {
> > if (de == "." || de == "..") continue;
> > st = lstat(fpath = path + "/" + de);
> > print(indent, i==sz-1 ? "`-- " : "|-- ", de);
> > if ((st[ST_MODE] & S_IFMT) == S_IFLNK) print(" -> ", readlink(fpath));
> > print("\n");
> > if ((st[ST_MODE] & S_IFMT) == S_IFDIR) {
> > [files, dirs] += tree(fpath, indent + (i==sz-1? " " : "| "));
> > dirs++;
> > } else files++;
> > }
> > return { files, dirs };
> > }
> > ----
> >
> > I could probably make it half as big by removing some of the extra bits.
> >
> > - Steve
> > _______________________________________________
> > Indiana Programming Group <Ipg at marilyn.indstate.edu>
> > http://marilyn.indstate.edu/mailman/listinfo/ipg
> >
> _______________________________________________
> Indiana Programming Group <Ipg at marilyn.indstate.edu>
> http://marilyn.indstate.edu/mailman/listinfo/ipg
More information about the Ipg
mailing list