basic list understanding

Q&A's, tips, howto's
Locked
joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

basic list understanding

Post by 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/Introducti ... ding_lists

where he says:

Code: Select all

(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:

Code: Select all

#/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:

Code: Select all

ERR: list expected in function find-all : "\\w{4,}"
Am I correct to see this as a list:

Code: Select all

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

I tried w/ the regex 0 option, too:

Code: Select all

(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
Posts: 7
Joined: Fri May 25, 2012 5:15 pm

Re: basic list understanding

Post by Patrick »

From the 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:

Code: Select all

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

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: basic list understanding

Post by xytroxon »

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.)

Code: Select all

#/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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: basic list understanding

Post by cormullion »

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

Code: Select all

 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:

Code: Select all

(find-all {\w{4,}} vowels $it regex)
;-> ("oooo" "uuuuu")
Which is cool. (And not even Haskell! :)

joejoe
Posts: 173
Joined: Thu Jun 25, 2009 5:09 pm
Location: Denver, USA

Re: basic list understanding

Post by joejoe »

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!

Locked