Lisp, Why?, step #5

01/08/19, modified 01/08/19

Why do you want strings ? What do you use them for ? Why are you using strings for that ? And so following.

Mircea Popescu

For years, I have wanted to break free of the tools of the Inca; Python, Matlab, Octave. These go-to languages to quickly and easily make something sort of work. I'm now working in a position where another set of tools is used, Word, Outlook, Excel. These are a step worse. Each time, I trade short term gains for long term costs. And those costs are mounting. This cannot stand. So, I want to work towards some sort of solid ground (learn Ada, pin down gnat,  read ffa). And also, use Common Lisp for those cases where I would normally go for Python and friends.

Next part of the why, I want to make diagrams like so. I've found no tools to help me with this on the long term;

  1. GUI Based diagram editors; Every editor comes with handy ways to make boxes with text,  support to put links between these boxes  and to make a sort of layout. But, for every change you will have to work on the entire diagram. Plus I do not want to become a proficient mouse worker.
  2. Graphviz; It sort of works, diagrams can be entered but to layout a diagram you I'll need to perform a feedback loop (change a parameter (like the order of the links), make graph, look at result, change another parameter, ...). The problem is that this loop is needed for every change, or simply give up on layout and then why are you doing it?.
So I jumped to the conclusion to produce some LISP code whereby I can define pieces of text1, define links between the pieces, specify locations for the pieces of text and finally the code will produce a diagram.  I decided to use svg as the the output format of this code. First task; given a piece of text, produce a svg file with this text and a box around the text.
As svg is just xml, I thought why not just string together the pieces needed, concatenate these into one big text and output the resulting mess.  And so have a kapstok to start learning LISP. My first step was to delve into the lisp spec was to find the call to do help with the concatenation magic. My other step was to piece together some higher level parts. And now on to a re-think desing.
As for the code so far;
(defun strcat (a b) (concatenate 'string a b))
(defun spacedstrcat (a b) (concatenate 'string a " " b))

(defun strlistcat (list)(reduce (lambda (a b) (concatenate 'string a b)) alist))

(defun xmlresttag (tag content)
  (if (and content (> (length content) 0))
      (strcat content (strcat (strcat "</" tag) ">"))
      "/>"))

(defun xmlbegintag (tag) (strcat "<" tag))
(defun xmlattribute (attribute) )
(defun xmlattibutes (attributes &key id class)
  (strlistcat (map xmlattribute attributes))

(defun xmlelement (tag content attributes &key id class)
  (spacedstrcat(spacedstrcat (begintag tag) (xmlattributes attributes :id id :class class))
               (xmlresttag (tag content))))

(defun rectangle (x y width height &key (content "") id class)
  ())

(defun assert-equals (a b)
  (if (string= a b) ()
      (progn (pprint `("not equals" ,a ,b) )
             (cerror "assertion failed" "abort"))))

(defun test$strcat ()
  (assert-equals (strcat "a" "b") "ab")
  (assert-equals (strcat "this" " is a test") "this is a test"))

(defun test$spacedstrcat ()
  (assert-equals (spacedstrcat "a" "b") "a b")
  (assert-equals (spacedstrcat "this" "is a test") "this is a test"))

(defun test$xmlresttag ()
  (assert-equals (xmlresttag "first" "") "/>")
  (assert-equals (xmlresttag "second" NIL) "/>")
  (assert-equals (xmlresttag "hello" "world") "world</hello>"))

(defun test$xmlbegintag ()
  (assert-equals (xmlbegintag "first") "<first"))

(defun run-tests ()
  (test$strcat)
  (test$spacedstrcat)
  (test$xmlresttag)
  (test$xmlbegintag))

The whole artifact is resting on the weak shoulders of concatenate and the wrong concept of  merging pieces of text into bigger pieces of text. I am thinking that constructing lists of strings will be a big improvement and then have a function whereby the whole list is output as svg text.

  1. I'm  avoiding the terms string and sentence []

2 Responses to “Lisp, Why?, step #5”

  1. Probably you already realized: there is no need to concatenate the strings (a very expensive operation anywhere) "in advance".

    If they're part of a WWWtron or similar, they're to be thrown into the outgoing socket or wherever in the correct order, when needed, all you need is to walk the list which points to them in the requisite order.

  2. The whole idea behind lisp can be approximated by looking at ~any physics' teacher conversation with any kid ever. "How fast is the rock ?" "36." "Thirty-six whats ?!" "36 km/h" "What's that ?" "10 m/s" "And how heavy is it ?" "10" "10 whats?!" "10 kilograms" "So what's the energy ?" "Five hundred." "Five hundred whats!" and so following.

    You want to add "hurr" and "durr" like kids add 7 pounds with 9 newtons to get 16 sieverts. One step away from the mess is indeed lists of strings -- rather than forcing upon the multiverse a strange assumption of monostring you can just keep the various things apart. The next step is properly tagged data, such that the computer is permitted to share with you some reflection of the meaning intrinsic in the world. And so on.

Leave a Reply