Monday, November 30, 2009

The simplest syntax-case

Have spent lots of time trying to understand syntax-case, yet there are no simple examples. At least until I came across this post.

Small change for Iron Scheme -




(define-syntax is-nil?
(lambda (stx)
(syntax-case stx ()
((_ ()) #t)
((_ stx) #f))))

The importance of quasiquote

If you were like me and skipped through most of the chapter on quote, quasiquote and splicedquote, then you made a bad mistake. This part is particularily important as the whole macro business is dependent on this. So here's a refresher.

Very simply put quote just quotes its arguments. So, when we execute the following, we get the output as test.



(import (rnrs))

(define foo 'test)
(display foo)

Quasiquote allows us to do more. It allows us have a template which we can fill in with values. Consider the following.


(import (rnrs))

(define x 10)
(display `(list x ,x))

The output will be (list x 10). However, the advantage of quasiquote goes much beyond just templates. We can use it to debug our code better. 



(import (rnrs))

(define (fact n)
(cond
((or (= n 0) (= n 1)) n)
(else (* n (fact (- n 1))))))

(display (fact 5))
Now, we want to understand how fact works. So, let us re-write fact not to compute the value but to just print out the expanded s-expressions.

(import (rnrs))

(define (fact n)
(cond
((or (= n 0) (= n 1)) n)
(else `(* ,n ,(fact (- n 1))))))

(display (fact 5))

Now the output is more interesting - (* 5 (* 4 (* 3 (* 2 1)))). What quasiquote and unquote ended up showing us is how the expression is calculated. Now, the fact example is trivial but it does give us an insight into the computation involved. 

Related to quote is spliced-quote. If we use spliced-quote then the output of a list is expanded. So, 



(import (rnrs))

(define foo `(1 ,@(list 2 3)))
(define bar `(1 (list 2 3)))

(display foo)
(newline)
(display bar)
(newline)

 will give us the output as (1 2 3) and (1 (list 2 3)).

Monday, November 23, 2009

Scheme - let-syntax

It took me some time to understand what let-syntax does. After
spending much time on the net search for information, I finally
figured it out. let-syntax is the let counterpart of define-syntax.

What does that mean? It means you create a macro that is lexically
bound. That means, let-syntax acts just like let, but it works on
syntax rather than variables. This means we can use let-syntax to
create localized macros. Consider this example..

(import (rnrs))

(define (hw n)
(display n)
(newline))

(let-syntax ((hw
(syntax-rules ()
((_ 0) (begin
(display "Hello World")
(newline)))
((_ n) (begin
(display "Goodbye World")
(newline))))))
(begin
(hw 0)
(hw 1)))

(hw 0)
(hw 1)

The (hw 0) and (hw 1) inside let-syntax end up printing "Hello World"
and "Goodbye World", whereas the (hw 0) and (hw 1) outside the scope,
print 0 and 1.

Wednesday, November 18, 2009

Windows Forms in IronScheme

The Discussion thread on Iron Scheme's project is here. I spent some time, playing with the code, and changing the style. Here's what I got.


(import
(rnrs)
(ironscheme clr)
(ironscheme clr shorthand))

(clr-reference System.Windows.Forms)
(clr-reference System.Drawing)

(clr-using System.Windows.Forms)
(clr-using System.Drawing)

;; Macros

;; Macro to set a property
(define-syntax set-property!
(syntax-rules (:button :form)
((set-property! :button button text x y)
(let ((b button))
(with-clr-type ((b Button))
(b : Text = text)
(b : Location = (clr-new Point x y)))))
((set-property! :form form text)
(let ((f form))
(with-clr-type ((f Form))
(f : Text = text))))))

;; Main Function that runs the form
(define (run form controls b1 b2)
(let ((mc controls))
(begin
(with-clr-type ((mc Form+ControlCollection))
(mc : Add (b1))
(mc : Add (b2))))

;; SHOW FORM AND RUN PUMP
(clr-static-call System.Console WriteLine "Start")
(clr-static-call Application (Run Form) form)
(clr-static-call System.Console WriteLine "Stop")

;; REMOVE CONTROLS
(with-clr-type ((mc Form+ControlCollection))
(mc : Remove (b1))
(mc : Remove (b2)))))



;; SETUP EVENTS
(define (make-event-handler text)
(lambda (s e)
(display text)
(newline)))

(define mainForm_MouseEnter (make-event-handler "Enter"))
(define mainForm_MouseLeave (make-event-handler "Leave"))
(define btnGo_Click (make-event-handler "Go"))
(define btnStop_Click (make-event-handler "Stop"))

(begin
;; INITIALIZE
(define mainForm (clr-new Form))
(define btnGo (clr-new Button))
(define btnStop (clr-new Button))
(define mainControls (clr-prop-get Form Controls mainForm))

;; APPLY EVENTS
(clr-event-add! Form MouseEnter mainForm mainForm_MouseEnter)
(clr-event-add! Form MouseLeave mainForm mainForm_MouseLeave)
(clr-event-add! Button Click btnGo btnGo_Click)
(clr-event-add! Button Click btnStop btnStop_Click)

(set-property! :form mainForm "Hello World")
(set-property! :button btnGo "GO" 10 20)
(set-property! :button btnStop "STOP" 100 20)

(run mainForm mainControls btnGo btnStop)

;; REMOVE EVENTS
(clr-event-remove! Form MouseEnter mainForm mainForm_MouseEnter)
(clr-event-remove! Form MouseLeave mainForm mainForm_MouseLeave)
(clr-event-remove! Button Click btnGo btnGo_Click)
(clr-event-remove! Button Click btnStop btnStop_Click))