Posted by Eric Kidd
Sun, 01 Jul 2007 23:00:00 GMT
Programming in Ruby makes me happy. It’s a lovable language, with a
pleasantly quirky syntax and lots of expressive power.
Programming in JavaScript, on the other hand, frustrates me to no end.
JavaScript could be a reasonable language, but it has all sorts of
ugly corner cases, and it forces me to roll everything from scratch.
I’ve been trying to make JavaScript a bit more like Ruby. In particular, I
want to support Ruby-style metaprogramming in JavaScript. This would make it possible to port over many advanced Ruby libraries.
You can
check out the interactive specification, or look at some examples
below. If the specification gives you any errors, please post them in the comment
thread, and let me know what browser you’re running!
Read more...
Tags JavaScript, Macros, Planetary, Ruby | 9 comments
Posted by Eric Kidd
Fri, 16 Mar 2007 02:29:00 GMT
…and how to fake Lisp macros with Template Haskell
(I wrote this article in response to a comment by
sigfpe. You may find it pretty dry reading, unless you want to
build domain-specific languages in Haskell. Proceed at your own
risk.)
Haskell’s built-in Monad type has some serious limitations. We can fix those limitations using a number of advanced Haskell techniques, including Template Haskell, Haskell’s closest equivalent to Lisp macros.
We can illustrate the limitations of Monad with an example from math. In set theory, we can define a set by specifying how to compute each
element:
{ xy : x ∈ {1,2,4}, y ∈ {1,2,4} }
We can read this as, “the set of all xy, where x
is one of {1,2,4}, and y is one of {1,2,4}.” To calculate the
answer, we first multiply together all the possible combinations:
1×1=1, 1×2=2, 1×4=4, 2×1=2, 2×2=4, 2×4=8, 4×1=4, 4×2=8, 4×4=16
We then collect up the answers, and—because we’re working with sets–we
throw away the duplicates:
{1,2,4,8,16}
Can we do the same thing in Haskell? Well, using Haskell’s list
monad, we can write:
listExample = do
x <- [1,2,4]
y <- [1,2,4]
return (x*y)
But when we run this, Haskell gives us lots of duplicate values:
> listExample
[1,2,4,2,4,8,4,8,16]
Our problem: We’re using lists (which can contain duplicate
values) to represent sets (which can’t). Can we fix this by switching to
Haskell’s Data.Set?
import qualified Data.Set as S
setExample = do
x <- S.fromList [1,2,4]
y <- S.fromList [1,2,4]
return (x*y)
Unfortunately, this code fails spectacularly. A Haskell monad is required
to work for any types a and b:
class Monad m where
return :: a -> m a
fail :: String -> m a
(>>=) :: m a -> (a -> m b) -> m b
But Data.Set only works for some types. Specifically, it
requires that values of type a can be ordered:
data (Ord a) => Set a = ...
As it turns out, we can make Data.Set into a monad. But be
warned: The solution involves some pretty ugly Haskell abuse.
Read more...
Tags Haskell, Macros, Monads | 9 comments
Posted by Eric Kidd
Sat, 03 Dec 2005 16:30:00 GMT
Years ago, I looked at Ruby and decided to ignore it. Ruby wasn’t as
popular as Python, and it wasn’t as powerful as LISP. So why should I
bother?
Of course, we could turn those criteria around. What if Ruby were more
popular than LISP, and more powerful than Python? Would that be enough
to make Ruby interesting?
Before answering this question, we should decide what makes LISP so
powerful. Paul Graham has written eloquently about LISP’s virtues. But, for the sake of argument, I’d like to boil them down to two things:
- LISP is a dense functional language.
- LISP has programmatic macros.
As it turns out, Ruby compares well as a functional language, and it fakes
macros better than I’d thought.
Read more...
Tags LISP, Macros, Recommended, Ruby
Posted by Eric
Fri, 13 Sep 2002 04:00:00 GMT
I've recently been reading a lot of excellent essays on
programming language design by Paul Graham. Paul and I agree about
a number of things: (1) LISP is beautiful and powerful family of
languages, even by modern standards, (2) all existing dialects of LISP
are lacking a certain something, and (3) programmatic macros are a Good
Idea.
Read more...
Tags Hacks, LISP, Macros, Recommended | 4 comments