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

Topics - Sammo

#1
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
#2
newLISP newS / Manual Notes 10.2.1
March 29, 2010, 09:57:27 AM
In the documentation for 'explode', ...
; omit last chunk if too short
(explode '(a b c d e f g h) 2 true)  → ((a b) (c d) (e f))

should be:
; omit last chunk if too short
(explode '(a b c d e f g) 2 true)  → ((a b) (c d) (e f))
#3
Anything else we might add? / LISP Cycles (humor)
August 03, 2007, 04:58:37 PM
http://xkcd.com/297/">//http://xkcd.com/297/
#4
Anything else we might add? / pack-length-bytes
March 31, 2006, 10:48:14 AM
In case anyone's interested, here is function 'pack-length-bytes' to compute and return the number of bytes that a proper 'pack' format string would produce. It works by cleverly replacing the elements of the format string with the number of bytes for each element, then summing those numbers.
;; pack-length-bytes
;; return the number of bytes defined by a well-formed
;; 'pack' format string
;;
;; e.g.,
;; (pack-length-bytes ">ucc c s27 s4 n1 s3 n22 n2") --> 64
;; (pack-length-bytes "cccccs27s4n1s3n22n2") --> 64
;;
;; Knowing that 'replace' is destructive is the key to
;; understanding this function.
;;
;; The (cons 0 (map ...)) hack returns 0 for the
;; legal "" str-format.
;;
(define (pack-length-bytes str-format)
  (let
    ( TABLE '((">"  ""  )   ;order of table entries matters
              ("<"  ""  )
              ("ld" "4 ")   ;"ld" must precede "d"
              ("lf" "8 ")   ;"lf" must precede "f"
              ("lu" "4 ")   ;"lu" must precede "u"
              ("b"  "1 ")
              ("c"  "1 ")
              ("d"  "2 ")
              ("f"  "4 ")
              ("u"  "2 ")
              ("s"  " " )
              ("n"  " " ))
    )
  ;body of let
    (map (fn (x) (replace (x 0) str-format (x 1))) TABLE)
    (apply + (cons 0 (map integer (parse str-format)))) ))
#5
Anything else we might add? / Christmas Quiz by Newlisper
December 21, 2005, 11:08:57 AM
Because I can't figure out how to leave a comment at http://newlisper.blogspot.com">http://newlisper.blogspot.com, I'll post my answers here:



Newlisper's problem statements is:
Quote
Christmas quiz: four by four



Here's a version of a traditional puzzle to exercise your mental maths muscles before Christmas. In newLISP, define symbols zero through nine that evaluate to their integer equivalents. In other words, define symbols so that you can type this into a newLISP console:



> zero one two three four five six seven eight nine



and get the answer:



0 1 2 3 4 5 6 7 8 9



However, the restrictions are that you cannot use any digits other than 4, and you must use four 4s, no fewer and no more. You can also use all the arithmetic operators (and parentheses, obviously).



To get you started, here's the easiest one - zero:



(set 'zero (- 44 44))

My solutions are:
Quote(set 'zero  (- 44 44))

(set 'zero  (+ 4 4 (- 4) (- 4)))

(set 'one   (/ 44 44))

(set 'two   (/ (* 4 4) (+ 4 4)))

(set 'two   (+ (/ 4 4) (/ 4 4)))

(set 'three (/ (+ 4 4 4) 4))

(set 'four  (+ 4 (* 4 (- 4 4))))

(set 'five  (/ (+ 4 (* 4 4)) 4))

(set 'six   (+ 4 (/ (+ 4 4) 4)))

(set 'seven (- (/ 44 4) 4))

(set 'seven (- (+ 4 4) (/ 4 4)))

(set 'eight (/ (* 4 (+ 4 4)) 4))

(set 'eight (- (* 4 4) (+ 4 4)))

(set 'nine  (+ (+ 4 4) (/ 4 4)))
#6
Does anyone know of a windows function and associated DLL that will return a list of all running processs and/or applications — lists similar to those provided by Task Manager?



Thanks,

-- Sam
#7
Anything else we might add? / lambda?
November 05, 2005, 11:42:33 AM
lambda? returns true as expected in the following:



> (define (bob))

(lambda ())

> (lambda? bob)

true



Should lambda? also return true in the following?



> (define-macro (bob))

(lambda-macro ())

> (lambda? bob)

nil
#8
Anything else we might add? / regex
May 24, 2005, 03:14:04 PM
This



(replace "(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))" "1234567" "," 0)



returns



1,234567



instead of



1,234,567



The expression does the right thing (i.e., returns 1,234,567) at this test site:



http://www.fileformat.info/tool/regex.htm">http://www.fileformat.info/tool/regex.htm



Look's like (replace ...) is replacing only the first instead of all occurences in v.8.5.9.



Thanks,

-- Sam
#9
Whither newLISP? / filter nil?
March 30, 2005, 02:57:55 PM
(filter if '(nil one nil two nil three)) --> (one two three)

(filter and '(nil one nil two nil three)) --> (one two three)

(filter or '(nil one nil two nil three)) --> (one two three)



Must be a better (that is, reads better) way to strip nils from a list, but I can't think of it now.



And, BTW, (filter not '(nil one nil two nil three)) --> (nil nil nil)



Thanks,

-- Sam
#10
Anything else we might add? / random-sequence
December 08, 2004, 01:16:20 PM
To generate a random sequence of integers, this seems to work well. Is there a better way?
(define (random-sequence n m)
    (letn ( s (sequence n m) j (length s) )
        (dotimes (x (* j 10)) (swap (rand j) (rand j) s))
        s ))
#11
newLISP and the O.S. / link to newlisp.dll
November 17, 2004, 10:49:28 AM
In a manner similar to


(link "newlisp.exe" "myProg.exe" "mySource.lsp")is there any chance that


(link "newlisp.dll" "my.dll" "mysource.lsp") would result in a usable 'my.dll' augmented with mysource.lsp's definitions?
#12
newLISP in the real world / memory leak in print?
September 29, 2004, 04:39:11 AM
Hello Lutz & Hans-Peter,



Using newLisp.dll with NeoBook via the hpwNewLisp plug-in, I am leaking memory at the rate of (I think) two bytes per call. With this test function:



(define (donothing) "test string")



and these calls:



a)  hpwNewLispCall "(donothing)" "[LispResult]"

b)  hpwNewLispCall "(silent (donothing))" "[LispResult]"

c)  hpwNewLispCall "(print (donothing))" "[LispResult]"

d)  hpwNewLispCall "(silent (print (donothing)))" "[LispResult]"



the c) and d) calls (both containing 'print') are leaking memory; the other calls are not.



The '(silent (print (myfunction)))' idiom is necessary to properly return values to NeoBook. I found this problem after writing a NeoBook program that invokes hpwNewLispCall every 200 ms. After running for several hours, Win32 complained about "running low on virtual memory." I found the two-bytes-per-call memory leak by experimentation and by watching Task Manager's Mem Usage window.



Can you help? Is the memory leak in newLISP's 'print' function, in the hpwNewLispCall interface, or elsewhere?



Thanks,

-- Sam
#13
newLISP in the real world / cat
August 05, 2004, 11:15:26 AM
Having a need to concatenate files and not having a decent command-line tool, I wrote:;;  cat
;;  concatenate files
;;  c:> cat file1 file2 file3 ... >stdout
;;
;;  cat.make
;;  (load {c:newlisplink.lsp})
;;  (link {c:newlispnewlisp.exe} "cat.exe" "cat.lsp")

(map (fn (F) (write-line (read-file F))) (rest (main-args)))
(exit)
which seems to work pretty well. Is there a better sol'n?
#14
Anything else we might add? / link a list of files?
July 28, 2004, 05:13:25 PM
Will (or should) the following work to extend link.lsp to allow several source files to be specified? I'd like to link several files (one main program plus several libraries) without having to merge the files by hand.


(load "link.lsp")
(define (link-list orgExe newExeName list-of-SourceNames)
  (set 'tempFileName "linktemp.lsp")
  (set 'tempHandle (open tempFileName "write"))
  (map (fn (F) (write-buffer tempHandle (read-file F))) list-of-SourceNames)
  (close tempHandle)
  (link orgExe NewExeName tempFileName))
after which I would execute


(link-list "newlisp.exe" "myprog.exe" '("mysource.lsp" "mylibrary.lsp"))I suppose a macro might be a better implementation choice, but being somewhat "macro challenged" I choose this route. A macro would have the advantage of not having to explictly form the list of files. Any other advantages?
#15
Anything else we might add? / integer from string
February 11, 2004, 10:12:31 AM
examples 2) and 4) seem counter-intuitive



1)  (integer "9") --> 9

2)  (integer "09") --> 0

3)  (integer "-9") --> -9

4)  (integer "-09") --> 0
#16
Anything else we might add? / benchmarks
February 08, 2004, 06:25:25 AM
There's a coding error in the loops benchmark. The benchmark code is (in part):(for (a 0 n)
   (for (b 0 n)
      (for (c 0 n))  <-- misplaced paren
         (for (d 0 n)
            (for (e 0 n)
               (for (f 0 n)
                  (inc 'x) )))))
It should be(for (a 0 n)
   (for (b 0 n)
      (for (c 0 n)
         (for (d 0 n)
            (for (e 0 n)
               (for (f 0 n)
                  (inc 'x) )))))) <-- moved paren
#17
Anything else we might add? / arrays
February 08, 2004, 06:18:23 AM
A couple of array questions:



1)  Is 'length' supposed to work directly on arrays? It seems to work but other list-related functions (e.g., 'first') understandably don't. The 'length' entry in manual version 7.5.2 refers to lists and strings but not arrays.



(set 'a (array 5 5))

(length (array-list a)) => 5

(length a) => 5



2)  Is there or could there be a way to determine an array's structure -- something like:(define (array-dims a , dims)
    (while (array? a)
        (push (length (array-list a)) dims)
        (setq a (nth 0 a)) )
    (reverse dims) )
For example:



(set 'a (array 2 3 '(("magoo"))) )

(array-dims a) => (2 3)
#18
Anything else we might add? / anagrams
January 22, 2004, 07:35:42 PM
Inspired by someone in the NeoBook forum to generate anagrams, I wrote this:(define (anagrams s)
    (if (<= (length s) 1)
        (list s)
        (flat (map (fn (n) (aux (rotate-string s n))) (sequence 1 (length s))))))

(define (aux rs)
    (map (fn (x) (append (first rs) x)) (anagrams (rest rs))))

(define (rotate-string s n)
    (join (rotate (explode s) n)))
which generates



    (anagrams "abcdefgh") --> 40,320 anagrams in 5.6 seconds

    (anagrams "abcdefghi") --> 362,880 anagrams in 64.5 seconds



on my 500 MHz 192Mbyte laptop. Does anyone see any significant speed or code improvements?  What about the use of 'append' in the 'aux' function to prepend a character to a string?  Would 'string' be a better choice?  And how about that 'rotate-string' function?  It looks relatively expensive time-wise.
#19
Anything else we might add? / crc32
January 18, 2004, 02:47:24 PM
I translated the apparently straightforward crc32 algorithm available http://www.thoughtstuff.com/rme/crc.lisp">here.  A couple of questions:



From the original ...
(defun update-crc (crc buf)
  (declare (type (unsigned-byte 32) crc)
  (type (simple-array (unsigned-byte 8)) buf)
  (optimize speed))
  (setf crc (logxor crc #xffffffff))
  (dotimes (n (length buf))
    (let ((i (logand #xff (logxor crc (aref buf n)))))
      (setf crc (logxor (aref *crc-table* i) (ash crc -8)))))
  (logxor crc #xffffffff))
I wrote ...
(define (update-crc crc buf)
    (set 'crc (^ crc 0xFFFFFFFF))
    (dotimes (n (length buf))
        (set 'i (& 0xFF (^ crc (char (nth n buf)))))
        (set 'crc (^ (nth i CRC-TABLE) (<< crc 8))) )
    (^ crc 0xFFFFFFFF))
in which I interpreted '(ash crc -8)' to mean '(<< crc 8)'.  Did I get that right?  The original concludes with ...
(defun crc (buf)
    (update-crc 0 buf))

;;; output should be #xCBF43926
(defun test-crc ()
  (let ((a (make-array 9 :element-type '(unsigned-byte 8)
      :initial-contents '(#x31 #x32 #x33 #x34 #x35 #x36 #x37 #x38 #x39))))
    (crc a)))
from which I wrote ...
(define (crc buf)
    (update-crc 0 buf))

(define (test-crc)
    (crc "123456789"))
and I invoke this in the 'tk' environment with
(format "%08X" (crc "123456789"))
You'll notice that I'm computing the crc32 of a character string instead of an array of bytes. Assuming I translated the original's #x31 #x32 ... #x39 correctly into "123456789", I don't get the expected 0xCBF43926 result.
#20
Anything else we might add? / returning the empty list
January 13, 2004, 02:21:11 PM
shouldn't both return ()?



> (if true '() '())

()



> (if nil '() '())

nil