basic list understanding

Started by joejoe, May 30, 2012, 05:20:51 PM

Previous topic - Next topic

joejoe

Hi,



I seem to be missing a basic understanding of what a list is.



I am following cormullion's "Building a list" example here:



http://en.wikibooks.org/wiki/Introduction_to_newLISP/print#Building_lists">http://en.wikibooks.org/wiki/Introducti ... ding_lists">http://en.wikibooks.org/wiki/Introduction_to_newLISP/print#Building_lists



where he says:


(set 'vowels '("a" "e" "i" "o" "u"))
;-> ("a" "e" "i" "o" "u") ; an unevaluated list


so I am trying to find-all 4+ character strings in my list:


#/usr/local/bin/newlisp

(set 'vowels '("a" "ee" "iii" "oooo" "uuuuu"))

(println (find-all {w{4,}} vowels))

(exit)


But when I run the code, it says:


ERR: list expected in function find-all : "\w{4,}"

Am I correct to see this as a list:


("a" "ee" "iii" "oooo" "uuuuu") ?



I tried w/ the regex 0 option, too:

(println (find-all {w{4,}} vowels 0))


but I still get the same error.



This is probably my most common error I get trying to run code, so I know its important! :0)



Thank you for shedding light on this hurdle.

Patrick

#1
From the http://www.newlisp.org/downloads/newlisp_manual.html#find-all">manual it seems like you are using find-all wrong. If the input is a list, then you cannot use a regex, but must use a list-matching-pattern. If you want to match the elements in the list, then try filter or map. What I think you want to do can be done like this:


(filter (fn (x) (find-all {w{4,}} x)) vowels)

xytroxon

#2
Before you can advance, first take a step back...



Readable working long form code you can understand is preferable to confusing non-working short form code you do not understand. While true lispers find my code below abhorrent, true lispers also find the idea of using newLISP itself abhorrent! (Note: The best way to deal with "true lispers" is to ask why they haven't advanced to using Haskell ;o)



Below is the verbose (and mentally easiest) way to think about processing a list of items. Commit this expanded code form to memory. Later, you can then visualize in your mind how the more complex and or shorter forms should be functioning with your list.



I like this as my standard list processing code layout, as I can easily add debugging statements where needed.



Also I substituted length for find-all's regex to simplify the code. (Use on UTF-8 strings might be problematic.)



#/usr/local/bin/newlisp

(set 'vowels '("a" "ee" "iii" "oooo" "uuuuu"))

(dolist (item vowels)
(if (>= (length item) 4)     ; comparison test each item
(begin ; when test is true for item
; put multiple statements here
(println $idx ": " item)
)
(begin ; when test is false for item (actually nil in newLISP)
; put multiple statements here
(println $idx ": ---")
)
)
)

(exit)


And if I haven't totally messed it up in simplifying it, this code should work ;o)



-- xytroxon



Note: I like to use the wscite editor which nicely highlights matching beginning ( and ending  )  Ctrl-E then can be used to jump back and forth between the ( ) 's in complex nested ((()()))'s.
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

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

cormullion

#3
find-all, like many newLISP functions, works on both strings and lists, and has different syntax patterns for each:


syntax: (find-all str-regex-pattern str-text [exp [int-regex-option]])
 syntax: (find-all list-match-pattern list-lists [exp])
 syntax: (find-all exp-key list exp func-compare)

You can find strings in strings, and lists in lists. So you shouldn't mix them. The error message appeared to be saying that, since argument 3 was a list, argument 2 should be too.



But having said that, I don't think it's strictly true. Consider:


(find-all {w{4,}} vowels $it regex)
;-> ("oooo" "uuuuu")

Which is cool. (And not even Haskell! :)

joejoe

#4
Thanks very much,



Patrick - I see now why the list wasnt working now. I will read the manual entries more carefully now. Beautiful and elegant solution. Awesome! :0)



xytroxon - I go forward w/ your sage advice as my new mantra. The dolist/if/begin/etc template in now in me! Thanks for that! It would seem to apply to about 90% of what I can imagine I am after. (im using geany on debian which looks to be a similar text editor to scite, thanks!)



cormullion - Way cool, ingenious and devious. I like it! Long live nL!