building html list from a newLISP list and then to nL list

Started by joejoe, November 13, 2009, 04:26:36 PM

Previous topic - Next topic

joejoe

i am trying to take a list of words and transform it into an html list.



the list:


one
two
three


what I am trying to construct is this:


<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>


what i am trying is this:


(set 'l (read-file "./list"))
(parse l)
(replace $it l "<li><a href="http://xyz.com/$it">$it</a></li>")
(println l)


however, i am getting the error:


ERR: string expected in function replace

the manual entry for the replace function says:



syntax: (replace exp-key list exp-replacement [func-compare])



i thought $it is the exp-key, l is the list, and the exp-replacement to be:


"<li><a href="http://xyz.com/$it">$it</a></li>"

am i correct to guess that replace is the correct function to do this?



thanks very much!

xytroxon

#1
Use format function, doing string %s replacement(s)...


; (set 'input (read-file "./list"))
; Note: For my test, I will use this string below instead.
(set 'input "onerntwotthree four")

; format list items using %s (twice), push to output list
(dolist (item (parse input))
(push (format "<li><a href="http://xyz.com/%s">%s</a></li>" item item) output -1)
)

; display as list
(println output)
(println)

; display each string
(dolist ( item output)
(println item)
)
(exit)


("<li><a href="http://xyz.com/one">one</a></li>" "<li><a href="http://xyz.com/two">two</a></li>"
 "<li><a href="http://xyz.com/three">three</a></li>" "<li><a href="http://xyz.com/four">four</a></li>")

<li><a href="http://xyz.com/one">one</a></li>
<li><a href="http://xyz.com/two">two</a></li>
<li><a href="http://xyz.com/three">three</a></li>
<li><a href="http://xyz.com/four">four</a></li>


-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

cormullion

#2
The system variables like $it are set as the result of some function running, so you use them 'after' rather than 'before'. "During executions of the replacement expression, the system variable $0 and the anaphoric system variable $it are set to the expression to be replaced."



I like the format approach, but you can stick with replace and $it:


(set 'lst '("one" "two" "three"))
(dolist (i lst)
  (replace i lst (string "<li><a href="http://xyz.com/" $it "">" $it "</a></li>")))


but it seems a bit clumsy to me.



How about:


(set 'lst '("one" "two" "three"))
(map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)

joejoe

#3
Quote from: "xytroxon"Use format function, doing string %s replacement(s)...



; format list items using %s (twice), push to output list
(dolist (item (parse input))
(push (format "<li><a href="http://xyz.com/%s">%s</a></li>" item item) output -1)
)
(exit)

-- xytroxon


!! awesome thanks xytroxon -- im really diggin the format function now. :0)

joejoe

#4
Quote from: "cormullion"The system variables like $it are set as the result of some function running, so you use them 'after' rather than 'before'. "During executions of the replacement expression, the system variable $0 and the anaphoric system variable $it are set to the expression to be replaced."



I like the format approach, but you can stick with replace and $it:


(set 'lst '("one" "two" "three"))
(dolist (i lst)
  (replace i lst (string "<li><a href="http://xyz.com/" $it "">" $it "</a></li>")))


but it seems a bit clumsy to me.



How about:


(set 'lst '("one" "two" "three"))
(map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)


thanks supremely cormullion!



the dolist now makes full sense to me and the map and fn are coming clear! :-)



thanks for the rapid refire which provided two educational examples.



i appreciate the help from both you cormullion, and xytronox on this!



i feel like im being assisted by two benign space invaders. :D

joejoe

#5
to make use of the above methods, im at a wall.



once something has been changed with map, i cant seem to get the new list out of it to do something with.



i tried:


(set 'lst '("one" "two" "three"))
(set 'list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))
(println list)
(exit)


but it gives:


ERR: symbol is protected in function set : list

i also tried set-ref-all and list and define to get the new list back out:



i replaced the second line above with both of these:


(set-ref-all newlst (list list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst)))

and


(define list (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))

am i anywhere close on any of these? thanks kindly again.

itistoday

#6
Quote from: "joejoe"but it gives:


ERR: symbol is protected in function set : list


'list' is a built-in function. You can't say (set 'list something), just pick a different symbol name.
Get your Objective newLISP groove on.

joejoe

#7
Quote from: "itistoday"
Quote from: "joejoe"but it gives:


ERR: symbol is protected in function set : list


'list' is a built-in function. You can't say (set 'list something), just pick a different symbol name.


rodger that --



wont ever make that mistake again!



thanks for the reply, itistoday.

joejoe

hmm... still doesnt seem to get the new list out with all the pieces.



this:


(set 'lst '("one" "two" "three"))
(set 'hlist (map (fn (item) (println {<li><a href="/} item {">} item {</a></li>})) lst))
(println hlist)


replies:


<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
("</a></li>" "</a></li>" "</a></li>")


which i think means that hlist is the last line above.



i know im closer than ever.



does the map function output directly into the set as tried here? it seems to set the list variables each as


"</a></li>"

cormullion

This is something that's easy to trip up over if you're not fully converted to functional programming... :) Every function returns a value. println returns a value. println also has a useful side-effect - it prints stuff. But it returns a value too.



In the newLISP console, type this:


>(set 'item "one")
"one"
> (println {<li><a href="/} item {">} item {</a></li>})
<li><a href="/one">one</a></li>
"</a></li>"


You'll see that, after the printing, the value returned by the println expression is "</a></li>" - the last value produced. So although the output was what you wanted, the return value wasn't. And it's that return value that is collected up and stored by map that you've stored in the list. Hence the collection of strings you've got in your hlist.



If you want the output to be printed and the same output to be returned as a value, you have to take steps to output and return the same string:


(set 'hlist (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))

The string function returns a value, which is printed by println and returned by println as the result of the anonymous function. So your result will be the strings in a list:


("<li><a href="/one">one</a></li>" "<li><a href="/two">two</a></li>" "<li><a href="/three">three</a></li>")

Lutz

... or, when you are really not that interested in the return value, perhaps a 'dolist' is more appropriate:


(dolist (item lst) (println {<li><a href="/} item {">} item {</a></li>}) )

joejoe

i apologize up front for not getting the concept.



i still cant get the html to be set to a variable that i can then use.



i tried with the code Lutz put up:


(set 'lst '("one" "two" "three"))
(set 'html (dolist (item list) (println {<li><a href="/} item {">} item {</a></li>}) ))
(println html)


but when i try to set it to a variable, as i added in above, it results in:


ERR: list expected in function dolist : list

i also tried the example cormullion wrote:


(set lst '("one" "two" "three"))
(set 'html (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))
(println html)


result here

:
ERR: symbol expected in function set : lst

in case i misread Lutz's post, i also tried this with (i think) the correct list:


(set 'lst '("one" "two" "three"))
(set 'html (dolist (item lst) (println {<li><a href="/} item {">} item {</a></li>}) ))
(println html)


which results in:


Quote<li><a href="/one">one</a></li>

<li><a href="/two">two</a></li>

<li><a href="/three">three</a></li>

</a></li>


and i think the fourth line is the 'html list, which hasnt been populated or something?



im struggling for sure. :]



gracious thanks again !

joejoe

i got it!



my mistake on not putting the ' in front of the symbol when adding to cormullions code.


(set 'lst '("one" "two" "three"))
(set 'html (map (fn (item) (println (string {<li><a href="/} item {">} item {</a></li>}))) lst))
(println html)


results in this:


<li><a href="/one">one</a></li>
<li><a href="/two">two</a></li>
<li><a href="/three">three</a></li>
("<li><a href="/one">one</a></li>" "<li><a href="/two">two</a></li>" "<li><a href="/three">three</a></li>")


woo hoo!!! :D

xytroxon

Edited: LOL!!! Your 2nd post beat mine... Do you want a list of strings or one html string???



If you want one string, then:



(set 'lst '("one" "two" "three"))

(set 'html "")

(dolist (item lst)
   (push (format "<li><a href="http://xyz.com/%s">%s</a></li>rn" item item) html -1)
)

(println html)
(exit)


Note 1: html MUST be either a preexisting string value or set to the empty string "" when pushing strings to it, otherwise, html will become a list of strings!



Note 2: The format string ends with the Windows' newline rn, remove or change for your system's newline char(s).



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

joejoe

Quote from: "xytroxon"Edited: LOL!!! Your 2nd post beat mine... Do you want a list of strings or one html string???


in the end, one string that i can put into an existing html file, replacing a similar one.



right now, i appreciate them both because its teaching me lots! :0)


Quote from: "xytroxon"If you want one string, then:



(set 'lst '("one" "two" "three"))

(set 'html "")

(dolist (item lst)
   (push (format "<li><a href="http://xyz.com/%s">%s</a></li>rn" item item) html -1)
)

(println html)
(exit)

thats awesome and simple (in the best way!). thanks for yet another way!!


Quote from: "xytroxon"Note 1: html MUST be either a preexisting string value or set to the empty string "" when pushing strings to it, otherwise, html will become a list of strings!



Note 2: The format string ends with the Windows' newline rn, remove or change for your system's newline char(s).



-- xytroxon


gotcha - im starting to see things more clearly.



much appreciated and thanks to all for the help with this.