[Haskell-cafe] [ANNAUNCE] ghcjs-0.1.0 Haskell to Javascript compiler

Martijn Schrage martijn at oblomov.com
Wed Oct 27 08:30:41 EDT 2010

On 21-10-10 01:01, Victor Nazarov wrote:
> I've been working on this for some month and I think now I'm ready to
> share the results.
Great stuff! I've been looking for something like this for a long time.

If you add "|| transport.status == 0" to line 90 of 
examples/rts-common.js, it also works on a local file system.

I played around with it a bit to see how easy it was to call JavaScript 
from Haskell, and it turned out to be straightforward. With a little 
guessing, I constructed a JavaScript representation of a thunk, which 
evaluates its string argument as a JavaScript expression and returns the 
resulting string to Haskell. This thunk can be passed to the 
JavaScript-compiled Haskell function. To pass it around in Haskell and 
force its evaluation, I constructed a simple monad.

Now, on your web page, you can do something like:

<input type="text" onkeyup="execHaskell('validate')" id="inputField"/>

and in a Haskell module:

validate :: JS ()
validate =
  do { inputValue <- eval "document.getElementById('inputField').value"
     ; exec $ 
              color inputValue
  where color str = if and (map isDigit str) then "'white'" else "'red'"

This example creates a text field that turns red if it contains any 
non-digit characters. It is on-line at 
http://tryout.oblomov.com/ghcjs/ghcjs.html  (Note: I only tested it on 
Firefox on a Mac)

All used files are in a zip file at 
http://tryout.oblomov.com/ghcjs/ghcjs.zip (validate is in Test.hs, the 
JS monad in JS.hs, and the JavaScript for execHaskell in util.js)

Martijn Schrage -- Oblomov Systems

More information about the Haskell-Cafe mailing list