OK, I realize that until there’s a release, some people might consider this hype. But since a few skeptical readers have asked, “where’s the code?”, here’s the ubiquitous currency converter example, ported from Phillipe Mougin’s F-Script to Nu.
;; converter.nu
;; Ubiquitous Cocoa currency converter example ported to Nu.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
(class ConverterController is NSObject
(ivar (id) window (id) form)
(imethod (id) init is
(super init)
(set @window ((NSWindow alloc)
initWithContentRect:'(125 513 383 175)
styleMask:(+ NSTitledWindowMask
NSClosableWindowMask
NSMiniaturizableWindowMask)
backing:NSBackingStoreBuffered
defer:0))
(@window setTitle:"Currency Converter")
(set @form ((NSForm alloc) initWithFrame:'(15 70 348 85)))
('("Exchange Rate per $1"
"Dollars to Convert"
"Amount in Other Currency") each:
(do (text) (@form addEntry:text)))
(@form set: (interlineSpacing:9
autosizesCells:1
target:self
action:"convert:"))
((@window contentView) addSubview:@form)
((@window contentView) addSubview:
((NSBox alloc) initWithFrame:'(15 59 353 2)))
(let (button ((NSButton alloc) initWithFrame:'(247 15 90 30)))
(button set: (bezelStyle:NSRoundedBezelStyle
title:"Convert"
keyEquivalent:"\r"
target:self
action:"convert:"))
((@window contentView) addSubview:button))
(@window orderFront:nil)
self)
(imethod (void) convert: (id) sender is
((@form cellAtIndex:2) setStringValue:
((* ((@form cellAtIndex:0) floatValue)
((@form cellAtIndex:1) floatValue))
stringValue))))
I would like to think that this is self-explanatory to a Cocoa programmer. Let this be the first test. Questions? Surprises? Suggestions?


11 comments ↓
This is a very interesting idea but it already has the same short-comings as lisp. Mainly the fact that everything is in (parentheses).. It really needs to be broken up more then that, something like
class ConverterController is NSObject
instance_variables one, two, ...
end
would be more readable.
Ah, the siren song of SYNTAX! Actually s-expressions have a siren song too I guess.
I would like to know about that those uses of “is”. What are “is”, “set”, and “let”? Is it that “is” has something to do with inheritance, “set” is for instance variables, and “let” is for local variables?
Michael, yes, I’ve been seduced by the power of s-expressions. This project would be much more difficult if it also required a grammar that could be safely extended with each new language feature. Oops, I mean, one other than s-expressions.
Thanks for your questions.
I’m using is in two ways. In the first, it follows a class name and indicates that the next symbol is the name of the class’s superclass. This is for use when a class is not already defined in the ObjC runtime. If the class already exists (either from compiled ObjC code or because it was defined somewhere previously in Nu code), then the is superclass can be omitted.
The second use of is is to mark the end of a method signature and the beginning of its implementation. I suppose that I could work harder and do that implicitly, but using the is keyword kept everything simple and seemed to have a reasonable semantic meaning.
set is actually used for several different types of assignment, depending on the name given by the first argument, which is quoted (not evaluated). If the first character of the argument is ’$’, the name’s value is set globally. If the first character is ’@’, the name is treated as an instance variable and an appropriate assignment is attempted. Otherwise, set is setting a local variable.
let is just a local form of set that strictly should only define names within the scope of the let statement, but currently the implementation of that is not strict. It’s mainly just for syntactic convenience, and I’d be happy to either tighten its semantics or drop it.
I’m looking forward to seeing this! Good luck with this project… I use newLISP (a simplified form of Lisp) for much day to day scripting work. I think that once you working with the syntax of a Lisp-like language (rather than just glaring at it), you start to enjoy the unique combination of simplicity, power, and elegance. And those parentheses start to become really useful if you’ve got a good text editor; I wish it was as easy to select the different parts of a phrase, sentence, or paragraph of English prose as it is to select the different components of a piece of Lisp code.
I’ve recently been wondering what a dynamic language w/ extremely good C integration would look like. After some scribbling, I had to conclude, isn’t this just Objective-C?
I like your idea, but, isn’t
just
in ObjC?
Jim,
Yes, on the surface Nu looks a lot like Objective-C; that was my goal. In Ruby, that same line would be: and the syntactic ugliness is just the tip of the iceberg of mismatches.But unlike Objective-C, Nu is interpreted. That’s a big difference. It means that Nu can be typed into a shell or a console built into an Objective-C application, and Nu can be reloaded on the fly into a running app.
Good point. Scheme48 & Ch might be worth a glance. Best of luck w/ your project!
You’re right, Jim, but consider the fact that apart from the sheer ease of introspection, you can actually modify the contents of those S-expressions during runtime, while a ObjC program is hopelessly static in this regard.
Yes, LISPers do resort to that kind of thing :)
And actually this is one of the reasons why LISP is so suited to AI programming: an algorithm can modify it’s own behaviour when it learns more about the surrounding environment, in a way that’s much, much more powerful than just fiddling with parameters.
HTH :)
You are one of the many people harming lisps progress by working on your own toy version, There are real compilers out there which need love. Hack on the C interface, write a bridge (look at the success Ruby/ObjC bridges have had). The benefits of this are far far greater for everyone than being able to write Objective-C in lisp syntax.
Can you really justify this? If not please try to collaborate your efforts with the rest of the world, instead of storming off so you can possess your own version lisp.
magnus: This is ridiculous. Tim has written tutorials for one Ruby/ObjC bridge and built another himself. Now he’s doing a “domain-specific” variant of Lisp geared specifically towards Objective-C because there’s a need for it.
Lisp fans often ascribe the absence of its success (or at least widespread acceptance and acknowledgement) to there not being one true Lisp. I consider this bullshit. Lisp is not only a programming language, it is its own branch of programming languages. One language isn’t going to fit all – even if you subscribe to the notion that out of all the languages, Lisp is the best (and I personally don’t), there will need to be room for variation.
How would one harm Lisp’s progress? For starters, one way would be to make sure there’s only one Lisp variant, and demand that all resources spent on anything anywhere near Lisp is pooled there, ridding the world of hundreds of useful influences of Lisp to other languages or of other languages to Lisp, and coming off looking like complete asshats for not letting people use whatever they wish of the collective knowledge of humanity in their own projects without needing to ask anyone for permission. This is a dead certain way of harming Lisp’s reputation, in return also harming uptake and interest.
Get off your high horse and let people do what they wish. If you want “one true Lisp”, why not join the existing efforts to do so? And if you disagree with them, well, aren’t you glad there’s more Lisps than “one true Lisp”?
—
As much as I’m usually left with a bad taste in my mouth after seeing S-expressions, there are ideas I wouldn’t mind spreading, like the mass-invocation of setters. Good work.
Although Nu might seem like a radical new thing, it’s actually really conservative. I built it by reusing Objective-C code and patterns that predate most (all?) of the popular scripting languages used today. Objective-C predates Ruby, Python, Javascript, even Tcl and Perl. It even predates Common Lisp. But I didn’t build on Objective-C out of some sense of moral obligation, I did it because Objective-C is a solid, reliable platform with good libraries, excellent performance, and a proven technology for dynamic binding.
Leave a Comment (sign in with Twitter)