Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - Sammo

#1
Thanks, cormullion, for the idioms using exists? and for-all. They are certainly cleaner than my "(apply and (map ...) version.



But, Kazimir,  your solution is precisely what I was seeking. After creating the various macros, the syntax is as simple as can be, and, by expanding as an explicit and expression and then evaluating that expression, the function returns nil as soon as it encounters an argument for which the predicate is not true.



Thank you very much.

-- Sam
#2
Is there a simple and readable way of applying a predicate to multiple arguments? Perhaps something like:
(all? integer? 1 2 3 "string")
I know I can write:
(apply and (map integer? (list 1 2 3 "string")))
but, actually, I'd like to just write
(integer? 1 2 3 "string")
which would return false (in this case because not all args are integers), or true (when all args are, in fact, integers).



It would be great if all predicates accepted multiple arguments.



Thanks,

-- Sam
#3
You'll find that parse correctly handles "Fri Mar 25 07:52:56 2011" and other times with hours less than 8 am, but not "Fri Mar 25 08:52:56 2011". For an extreme case, try "Fri Mar 25 08:08:08 2011". I'll bet this is caused by newLISP's number parser which treats digit strings beginning with "0" as octal and breaking the parse on non-octal digits such as "8" and "9".
#4
newLISP in the real world / Re: Another challenge
March 07, 2011, 12:43:56 PM
This works but is ugly looking.
(map list (sort (flat (dup (sequence 1 5) 4))) (explode (dup "abcd" 5)))
With comments:
(map list
(sort (flat (dup (sequence 1 5) 4))) ;numbers
(explode (dup "abcd" 5)) ;letters
)

Edit: Simplified expression generating list of letters. Was "(flat (dup (explode "abcd") 5))."
#5
newLISP in the real world / Re: Objects
March 06, 2011, 04:38:43 AM
QuoteCan you explain why we need letex in this case? Why lambda can't take side from function definition?

I am neither a Scheme nor newLisp expert, but I believe the significant difference between them (for this example) lies in a concept called http://www.c2.com/cgi/wiki?LexicalClosure">lexical closures.



When Scheme executes your make-square code, it creates (by way of the lambda expression) a closure in which the variable 'side' is bound to the value provided at execution time. When the resulting closure is then executed, the binding is reestablished and the expected result is produced.



newLisp doesn't implement closures (at least in the Scheme way), so we 'trick' it into forming a "hard closure" by rewriting the expression (i.e., substituting a fixed value for 'side') by way of the 'letex' function.



Without the 'letex' wrapper, newLisp produces a function (i.e., lambda expression) with references to the variable 'side.' With the wrapper, newLisp produces a function with the variable 'side' replaced with the value provided when the function was created.



Michael's FOOP example illustrates how closures can be implemented in newLisp.



-- Sam



Edit 2011-03-07: Removed an extraneous word.
#6
newLISP in the real world / Re: Objects
March 05, 2011, 03:25:41 PM
A direct translation of the Scheme code to newLisp might be:


; make-square
; 2011-03-05
; example
; (set 's1 (make-square 2))
; (s1 'area) ;result = 4
; (s1 'perimeter) ;result = 8
; (s1 'xxx) ;result = "unknown message" error
(define (make-square side)
(letex (s side)
(lambda (msg)
(cond
((= msg 'area) (mul s s))
((= msg 'perimeter) (mul 4 s))
(true (throw-error "unknown message")) ))))

but study the FOOP section of the newLisp manual for stronger object-oriented techniques.

-- Sam
#7
newLISP in the real world / Re: This week's challenge
February 27, 2011, 08:53:06 PM
Probably not elegant but it works:
(define (magoo list-of-strings)
(magoo-aux (sort list-of-strings) '()))

(define (magoo-aux list-of-strings list-of-lists)
(if (empty? list-of-strings)
list-of-lists
  ;else
    (local (newlist)
(push (pop list-of-strings) newlist)
(while (and
(not (empty? list-of-strings))
  (= ((list-of-strings 0) 0) ((newlist 0) 0))
(push (pop list-of-strings) newlist -1))
)
(magoo-aux list-of-strings (push newlist list-of-lists -1))
)
)
)
#8
Works here running newLISP v.10.2.14 on WinXP. I get the following output:
("API" "About" "All" "Apps" "Art" "Built" "C" "Cilk" "Development" "Docs" "Documented"
 "Downloads" "Expandable" "Find" "Forum" "FriendFeed" "Friendly" "GNU" "GPL" "GS"
 "General" "Gui" "Home" "IDE" "It" "LISP" "LISPs" "Libs" "License" "Links" "Linux"
 "Lisp" "Mac" "Maintenance" "May" "Modules" "Most" "Movie" "Netscape" "News" "Nuevatec"
 "OS" "October" "Parallel" "Public" "Read" "Release" "Search" "See" "Server" "Share"
 "Slideshow" "Stable" "Tips" "Tricks" "Twitter" "UA" "UBUNTU" "Unix" "Watch" "Win"
 "X" "a" "about" "all" "already" "amp" "and" "are" "area" "arial" "b" "background"
 "big" "bold" "border" "built" "but" "button" "buttons" "by" "ccc" "changes" "code"
 "color" "complete" "computing" "copy" "copyright" "courier" "custom" "dashed" "ddd"
 "decoration" "differs" "displating" "distributed" "divs" "doing" "easier" "edit"
 "eee" "eef" "entry" "ever" "example" "family" "fast" "features" "files" "follow"
 "font" "for" "friendly" "from" "functions" "general" "has" "helvetica" "hover" "how"
 "hr" "if" "img" "in" "installers" "is" "language" "layout" "learn" "left" "libraries"
 "like" "line" "links" "list" "magic" "margin" "mdash" "mediumred" "modern" "modes"
 "modules" "more" "most" "mybox" "need" "new" "newLISP" "none" "normal" "not" "of"
 "on" "operating" "or" "other" "out" "overwritten" "p" "padding" "passwords" "powered"
 "processing" "purpose" "px" "quick" "referencrs" "reserved" "right" "rights" "runs"
 "sans" "save" "scripting" "search" "separator" "serif" "server" "shared" "site"
 "size" "small" "smallred" "solid" "spec" "st" "style" "styles" "support" "systems"
 "table" "tables" "tag" "tags" "td" "text" "textarea" "th" "the" "these" "thinkHome"
 "to" "top" "tr" "traditional" "transparent" "uacct" "urchinTracker" "use" "used"
 "using" "v" "verdana" "version" "weight" "when" "width" "wiki" "will" "with" "work"
 "written" "you")
#9
Would this work?
> (set 'record '(("name" "Tim")(age 61)))
> (set 'ndxs '(a b))
> (set 'a 1 'b 0)
> (record (map eval ndxs))
age
#10
Another solution:
(transpose (list a b))
>((fred 1) (jim 2) (bob 3))

and applying transpose again has the effect of undoing:
(transpose (transpose (list a b)))
>((fred jim bob) (1 2 3))
#11
newLISP in the real world / Re: (parse) oddness
April 20, 2010, 08:15:13 PM
On Win XP running newLisp 10.2.1, I dup'd your file (complete with n instead of rn) but could not duplicate your symptom.
(parse (read-file {C:MyDirectorysites-sm.txt}) "n") returns
("1tgoogle.com" "2tfacebook.com" "3tyahoo.com" "4tyoutube.com" "5tlive.com"
 "6twikipedia.org" "7tblogger.com" "8tbaidu.com" "9tmsn.com" "10tyahoo.co.jp")

The problem is either in a newLisp version later than 10.2.1 or in a non-Windows version.
#12
newLISP in the real world / Re: (parse) oddness
April 20, 2010, 03:48:29 PM
If there isn't a trailing "n" in "sites-sm.txt", then is one being appended in (read-file "sites-sm.txt")? It seems that it would have to be in order for trim to correct the problem.

-- Sam
#13
newLISP in the real world / Re: (parse) oddness
April 20, 2010, 03:27:44 PM
A trailing "n" in "sites-sm.txt" is the culprit. Try removing the trailing newlines before parsing:
(set 'sites (parse (trim (read-file "sites-sm.txt") "" "n") "n"))

Edit: Corrected typo
#14
newLISP in the real world / Re: URL Encode / decode
April 06, 2010, 11:20:04 AM
In the Code Snippets example:
(define (url-decode str)
  (replace "+" str " ") ; optional
  (replace "%([0-9A-F][0-9A-F])" s (char (int $1 0 16)) 1))

should be
(define (url-decode str)
  (replace "+" str " ") ; optional
  (replace "%([0-9A-F][0-9A-F])" str (char (int $1 0 16)) 1))

in which 's' in the second replace is changed to 'str'.



-- Sam
#15
Maybe ref-all to find reference vectors for all elements, then take the longest vector as the complexity metric? Something like this:
(apply max (map length (ref-all nil aList (fn (x) true))))

Edit: Clarified list parameter in ref-all.