Most who know me know that I'm a Java programmer through and through. But, taking the advice of Pragmatic Dave (and others), I occasionally tinker with other languages in order to stretch my brain and give me a new insight on how to solve problems.
To that end, I took about a very brief break from my otherwise Java-filled world and downloaded DrScheme, so that I could try my hand at Lisp/Scheme (could someone explain to me what the difference is?).
I had played around with Lisp in college and I've been known to occasionally modify my Emacs editor by editing Lisp, but truthfully, I never have really known what I'm doing when it comes to Lisp. But my experience with DrScheme was different.
In no time at all, with only a small amount of help from the online documentation, I had written the following:
(require (lib "framework.ss" "framework"))
(require (lib "framework-sig.ss" "framework"))
(require (lib "framework-unit.ss" "framework"))
(require (lib "macro.ss" "framework"))
; Create a frame
(define frame (instantiate frame% ("FOO")
(width 400) (height 300)))
; Make the frame visible
(send frame show #t)
; Create a text label
(define msg (instantiate message% ("" frame)
(stretchable-width true)))
; Create a text-field that is labeled with "Your name"
(define myText (instantiate text-field%
("Your name" frame void)))
; Create a "Do something" button that copies the value
; of myText to msg
(instantiate button% ("Do Something" frame (
lambda (button event)
(send msg set-label (send myText get-value)))))
Oddly enough, I actually understand most of what is going on in this snippet of code. The only thing I'm not 100% clear on is what "lambda" means, but I at least understand what it's doing.
For you fellow Java programmers, here's a rough AWT equivalent of what's going on above:
Frame frame = new Frame("FOO");
frame.setSize(400,300);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
Label msg = new Label("");
frame.add(msg);
Label myText_label = new Label("Your name");
TextField myText = new TextField();
frame.add(myText_label);
frame.add(myText);
Button button = new Button("Do Something");
frame.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
msg.setText(myText.getText());
}
}
);
When I say that this is a rough equivalent, it means that I've not even tried to compile it. The layout is probably all wrong, but you should get the gist of it.
This silly exercise managed to break through many of the barriers I've had with understanding Lisp. It's kind of eye-opening to see how stuff like this is done outside of Java. I highly recommend that everyone break away from Java (or whatever your day-to-day language is) and try something different. It's really worth it.