wisp-lang / wisp

A little Clojure-like LISP in JavaScript
https://gozala.github.io/wisp/
Other
980 stars 68 forks source link

While Forever #86

Closed alexispurslane closed 10 years ago

alexispurslane commented 10 years ago

In the Wisp console, this code goes on forever:

(def i 0)
(while (< i 10)
    (.log console i)
    (set! i (+ i 1)))

I have tried it in the equivalent JavaScript in the Chrome Console and it worked fine! I otherwise LOVE the idea of Wisp, and will probably use it! I installed wisp using npm install -g wisp and started the repl using wisp. I am on a mac 13in retina 2013 running mavericks...

Gozala commented 10 years ago

I'm surprised it actually does compile into something runnable o_0. Anyway there is no while forms in wisp, in fact while here is assumed to be a function. If you paste your code into http://jeditoolkit.com/try-wisp/ you'll see output:

var i = exports.i = 0;
while(i < 10, console.log(i), i = i + 1);

I'm guess it turns out to be a vaid JS, although wisp just thinks of it as invocation of function named while with arguments (i < 10), (.log console i) and (set! i (+ i 1)))

Anyhow plain while loops & mutations isn't idiomatic way to write code in clojure(script) and inherently in wisp. This kind of loop would be expressed as follows:

(loop [i 0]
  (if (> i 9)
    (do
      (print i)
      (recur (+ i 1)))))

Note: Current compilation output isn't all that pretty I'm afraid although I do have plans on making a lot better.

markjreed commented 10 years ago

Since Clojure has a while, it seems wisp should as well... although the code above needs a (do ...) wrapped around the body to be valid Clojure.

On Sun, Mar 16, 2014 at 1:29 AM, Irakli Gozalishvili < notifications@github.com> wrote:

I'm surprised it actually does compile into something runnable o_0. Anyway there is no while forms in wisp, in fact while here is assumed to be a function. If you paste your code into http://jeditoolkit.com/try-wisp/you'll see output:

var i = exports.i = 0;while(i < 10, console.log(i), i = i + 1);

I'm guess it turns out to be a vaid JS, although wisp just thinks of it as invocation of function named while with arguments (i < 10), (.log console i) and (set! i (+ i 1)))

Anyhow plain while loops & mutations isn't idiomatic way to write code in clojure(script) and inherently in wisp. This kind of loop would be expressed as follows:

(loop [i 0](if %28> i 9%29 %28do %28print i%29 %28recur %28+ i 1%29%29%29))

Note: Current compilation output isn't all that pretty I'm afraid although I do have plans on making a lot better.

— Reply to this email directly or view it on GitHubhttps://github.com/Gozala/wisp/issues/86#issuecomment-37749347 .

Mark J. Reed markjreed@gmail.com

Gozala commented 10 years ago

It's unlikely while form will end up in wisp, although there is some effort being made to raise compile time warnings when attempting to forms not known to a compiler rather than trying to treat everything as a function call (see #93)