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 - ant

#1
newLISP in the real world / Re: variable capture?
November 17, 2011, 08:03:08 AM
Thanks again for the suggestions. I have a lot to learn!
#2
newLISP in the real world / Re: variable capture?
November 15, 2011, 12:27:17 PM
I think I'm starting to understand. Thank you every one who patiently explained and suggested alternatives.



I'm using macros because I'm passing large-ish objects around (bitmaps) and for performance reasons I don't want newLISP to create a copy of these objects every time I call a function passing one as an argument.



Possibly my problem was that when I read the doc on define-macro it said (still says) you can avoid variable capture by using (args n), but it didn't mention all the various caveats that I now have a little more understanding of thanks to this thread.
#3
newLISP in the real world / Re: variable capture?
November 14, 2011, 01:45:05 PM
I am a newLISP noob. There is almost nothing I understand of the culture. I thought I was using the style suggested in the article http://www.newlisp.org/downloads/newlisp_manual.html#define-macro">//http://www.newlisp.org/downloads/newlisp_manual.html#define-macro. I thought it meant: use letex with args to avoid variable capture. I'm still confused. I don't understand why only lst is affected. In fact, this


(define (test3 condition)
  (println "test3")
  (dolist-while (x '(a b c d e f) condition) (println x)))

(define (test4 cnd)
  (println "test4")
  (dolist-while (x '(a b c d e f) cnd) (println x)))

(test3 nil)
(test4 nil)


gives this



test3

nil

test4

a

b

c

d

e

f

f



In fact, I can't work out how to safely use args in a macro at all. E.g.


(define-macro (ant)
  (let ((x 3))
    (println "(args 0)=" (args 0) " x=" x " (eval(args 0))=" (eval(args 0)))))

(define (foo y)
  (ant y))
(define (bar x)
  (ant x))

(foo 7)
(bar 7)


gives



(args 0)=y x=3 (eval(args 0))=7

7

(args 0)=x x=3 (eval(args 0))=3

3
#4
newLISP in the real world / Re: variable capture?
November 14, 2011, 02:29:14 AM
Hi and thanks both for replying. I'm still trying to puzzle it out.



I don't quite get why your fix, Lutz, affects just the lst variable. Why aren't the other variables susceptible to the same problem?
#5
newLISP in the real world / variable capture?
November 13, 2011, 12:55:05 PM
I don't understand what I'm doing wrong here. Is it variable capture?



Using the example from the define-macro description in the newLISP doc I wrote two identical test functions, apart from the name of the parameter:


(define-macro (dolist-while)
  (letex (var (args 0 0)
          lst (args 0 1)
          cnd (args 0 2)
          body (cons 'begin (1 (args))))
    (let (res)
      (catch (dolist (var lst)
               (if (set 'res cnd) body (throw res)))))))

(define (test1 a-list)
  (dolist-while (x a-list (!= x 'd)) (println x)))

(define (test2 lst)
  (dolist-while (x lst (!= x 'd)) (println x)))

(test1 '(a b c d e f))
(test2 '(a b c d e f))


here is the output


newLISP v.10.3.3 on OSX IPv4/6 UTF-8, execute 'newlisp -h' for more info.

>
(lambda-macro ()
 (letex (var (args 0 0) lst (args 0 1) cnd (args 0 2) body (cons 'begin (1 (args))))
  (let (res)
   (catch
    (dolist (var lst)
     (if (set 'res cnd)
      body
      (throw res)))))))
(lambda (a-list) (dolist-while (x a-list (!= x 'd)) (println x)))
(lambda (lst) (dolist-while (x lst (!= x 'd)) (println x)))
a
b
c
nil

ERR: list expected in function dolist : lst
called from user defined function dolist-while
called from user defined function test2
>
#6
Yes, that demo is the point from which I started hacking. I'll pursue my bitmap effort a little further and see what FPS I get. I'll probably have further questions. If I think the end result is likely to be of any interest to anyone else I will make it freely available. Thank you again.
#7
Lutz, I appreciate you taking the time to answer my newbie questions.



I found this worked for me:


(define (set-pixel x y r g b)
(let (i (* (+ x (* y WIDTH)) 3))
(cpymem (pack "bbb" r g b) (+ (address rgb-buf) i) 3)))


Your suggestion meant rewriting the entire bitmap to change one pixel, which seemed inefficient. Also, for even a tiny 100 x 100 pixel bitmap I think you were suggesting I'd need a list of 30,000 cons cells. Wouldn't it be quite inefficient to access an arbitrary cell to change its value?



I'm attempting to implement a 2D game in newLISP, so performance will be important. It's probably a silly idea. But I'm hoping to learn more about LISP than I know now, which isn't much.



Ant
#8
Hi Lutz, thank you for the suggestion.



In view of what you say can you suggest the most appropriate data type to use to pass to glDrawPixels? I want to create a contiguous array of RGB binary byte values to pass to this function and I want to manipulate the contents of this buffer in LISP, to display bitmapped images. If I use strings should I be able to read and write arbitrary values to arbitrary locations in the string buffer?



In C I might use an array of unsigned chars. In newLISP what would be the equivalent of this?



    rgbbuf[index] = byte_value;
#9
I'm trying to set a character value in a string containing zeros. Is the following behaviour correct?



newLISP v.10.3.3 on OSX IPv4/6 UTF-8, execute 'newlisp -h' for more info.



> > (set 's "abc")

"abc"

> (setf (s 1) "X")

"X"

> s

"aXc"

> (set 's "000000")

"000000"

> s

"000000"

> (length s)

3

> (setf (s 1) "X")



ERR]
#10
newLISP Graphics & Sound / Re: glDrawPixels possible?
October 25, 2011, 12:39:34 PM
Hi Lutz, Thanks for the info. I'm trying it now but I haven't succeeded in getting anything on the screen yet. I'll keep at it. Ant
#11
newLISP Graphics & Sound / glDrawPixels possible?
October 24, 2011, 02:36:26 PM
Is it possible to use glDrawPixels from a newLISP script? I want to display an image I've constructed in an array of RGB values.



If it is possible, could you point me to the documentation or an example?



If it isn't possible, am I trying to do something newLISP is not designed for?