Solution to "how to convert a file to a list?"
Damien Diederen <dd@crosstwine.com>
Cf. http://tech.groups.yahoo.com/group/skill_school/message/2262 for a (succinct) description of the problem. Function MyTreeFromFile implements the requested functionality:
(println (MyTreeFromFile "./my-file.dat")) => (a (b) c (d (f)))
References:
From: *redacted* Date: Sun, 06 Jan 2013 17:09:50 +0800 Subject: how to convert a file to a list? List-Id: <skill_school.yahoogroups.com> Message-ID: <op.wqhfqojhuwf9wv@hamp-sz-0073.hamppcb.com>
(define chompCharacterBag ...)
Characters trimmed on the right.
(define indentCharacterBag ...)
Characters trimmed on the left and determining a particular line's "depth."
(defun DepthAndData (line) ...)
DepthAndData transforms a "raw" line into a list of two values: its integer depth, as indicated by the number of characters from indentCharacterBag, and the rest of the line, right-trimmed with chompCharacterBag.
(defun EmptyTconc () ...)
EmptyTconc returns a new, empty "tconc structure." Used for code readability; cf. tconc's documentation for a description of tconc structures.
(defun TreeFromFile (filename) ...)
Builds a tree (list of lists and symbols) by reading filename.
Levels are determined by looking at the number of dots (which must be >= 1) in front of each line. The rest of each line, after the dots and up to the return carriage, if any, is transformed to a symbol and inserted as a leaf at the right position. Example:
.a ..b .c => (a (b) c (d (f))) ..d ...f
Cf. package commentary for additional details.
(defun TreeFromLiner (liner) ...)
Builds a tree from liner, a function which returns "raw" lines, or nil on "EOF." Cf. TreeFromFile and package commentary for details.
(defun PortLiner (port) ...)
Builds a "liner" (a function which returns "raw" lines and nil on EOF), from port.
(defun TconcData (tc) ...)
TconcData retrieves the newly-built list from "tconc structure" tc. Used for code readability; cf. tconc's documentation for a description of tconc structures.
(defun TransformData (data) ...)
According to the example, tree leaves ought to be symbols.
(defun TrimLeftIndex (characterBag string a b) ...)
TrimLeftIndex indicates how to trim the substring of string delimited by a and b to remove all leading characters contained in characterBag.
Both a and b are inclusive. Use 1 and (strlen string) to trim the whole string.
Returns the starting index in the resulting substring.
(defun TrimRightIndex (characterBag string a b) ...)
TrimLeftIndex indicates how to trim the substring of string delimited by a and b to remove all trailing characters contained in characterBag.
Both a and b are inclusive. Use 1 and (strlen string) to trim the whole string.
Returns the last index in the resulting substring.