Basic questions about using joxa

121 views
Skip to first unread message

Ivan Uemlianin

unread,
Apr 6, 2013, 5:22:35 PM4/6/13
to jo...@googlegroups.com
Dear All

I have just started learning joxa --- I'm planning to work through The Little Schemer.  I have a few very basic questions, answers to which I did not find in the documentation.  Please could anyone answer, or help me find my way around the docs.

Two about the shell:

1. What is the correct way to quit from the shell?  I have only found ctrl-c and ctrl-d.

2. Is there a way to (re-)compile and (re-)load a joxa file from the shell?

Two about the language:

3. Are car, cdr and cons defined in joxa?

4. Is there a way to quote arbitrary stuff?  On p. 4 of The Little Schemer there's a question:

  Is it true that this is a list?

    (atom turkey) or

If I've defined an is_list function (using erlang's is_list), is there a way to express that question in joxa?

  (is_list ...?

One last question about my setup:

5. I think I might have missed/messed something.  The joxa documentation has examples like:

  joxa-is> (joxa-core/!= 1 2)
  :true
  
  joxa-is> (joxa-core/!= 1 1)
  :false

But in my shell I get:

joxa-is> (joxa-core/!= 1 2)
:0:0 *error* invalid reference joxa-core/!=/2
error

joxa-is> (joxa-core/!= 1 1)
:0:0 *error* invalid reference joxa-core/!=/2
error

Any help on any of the above much appreciated. When I finish chapter one, I'll write a blog post :D

With thanks and best wishes

Ivan

Klaus Trainer

unread,
Apr 6, 2013, 7:40:39 PM4/6/13
to jo...@googlegroups.com
Hi Ivan.

I'll try to explain one thing, which will probably explain most of your questions.

Joxa is an extremely small language (which is one of its greatest strengths IMO). There are only very few special forms. They are documented here: http://docs.joxa.org/en/latest/language.html. Note that even basic math operators are not built into the language. That means that in order to do something useful, you have to use functions that come along with Joxa's standard library (http://docs.joxa.org/en/latest/standard-library.html) or with the Erlang distributions, or use whatever Erlang library you need. So, how can you use those?

Quite central to Joxa is the concept of namespaces. At runtime, any Joxa code is executed in some namespace. For example, when you start the Joxa shell, the default namespace is `joxa-is`. You can declare a new namespace with (ns some-namespace-name). Erlang modules are treated as namespaces, i.e., the namespace name is equal to the module name name, however you can change those before using them, e.g. like this: (require (error_logger :as logger)). In order to use functions from other namespaces you have to either use the `require` or the `use` special form. With e.g. (require some-namespace) you just declare that you are going to invoke functions from `some-namespace`, whereas with e.g. (use some-namespace) you import every exported functions function from `some-namespace` into your own current namespace.


Below are my quick answers to your questions.



> Two about the shell:
>
> 1. What is the correct way to quit from the shell?  I have only found ctrl-c and ctrl-d.

You can use Erlang's `halt/0` function:

(use erlang)
(halt)


or even shorter, Erlang's `q/0` function.

(use c)
(q)



> 2. Is there a way to (re-)compile and (re-)load a joxa file from the shell?

Hah, that question was actually a bit tricky. AFAIK there's no equivalent to Eralng's `c/1` function, which allows you to compile and reload a file from the shell. However you could use the reloader module from mochiweb: https://github.com/mochi/mochiweb/blob/master/src/reloader.erl

Supposed that you have the compiled reloader beam-file (it can be compiled with `erlc reloader.erl` from the shell) in your current directory and the compiled Joxa distribution in some other directory (e.g. '~/dev/joxa'), you can start the Joxa shell by first invoking the Erlang shell like this:

    erl -pa ~/dev/joxa/ebin ~/dev/joxa/deps/*/ebin -s reloader

Then enter the following in order to actually start the Joxa shell:

    'joxa-shell':start().

Now the reloader will reload any Joxa or Erlang module in your current directory as soon as you have successfully compiled it. Btw., if you want this not only for your current directory but for another one as well, just add it to the '-pa' option when invoking `erl`. For example if you'd like to add the directory 'src', it'd look like this:

    erl -pa ~/dev/joxa/ebin ~/dev/joxa/deps/*/ebin src -s reloader



> Two about the language:
>
> 3. Are car, cdr and cons defined in joxa?

Nope. However, there are equivalent `hd/1` and `tl/1` functions in joxa-lists. As Joxa has macros, it only takes a few lines of code to provide your version of `hd/1` and `tl/1` called `car/1` and `cdr/1`:

(ns my-joxa-lists
  (require joxa-lists))

(defmacro+ car (l)
  `(joxa-lists/hd ~l))

(defmacro+ cdr (l)
  `(joxa-lists/tl ~l))



> 4. Is there a way to quote arbitrary stuff?  On p. 4 of The Little Schemer there's a question:
>
>  Is it true that this is a list?
>
>    (atom turkey) or

Of course. Just like in most other Lisps, there's the special form `quote`. So, instead of (atom turkey) you simply write (quote turkey). There's are also two different syntaxes that can be used equivalently: `'turkey` and `:turkey`. In Joxa it's kind of a convention to use the quote symbol for lists, e.g. '(foo bar baz), while the colon symbol is usually used for single atoms, e.g. :turkey.


> If I've defined an is_list function (using erlang's is_list), is there a way to express that question in joxa?
>
>   (is_list ...?

Of course, why not? You want a predicate function with a question mark? Nothing prevents you from creating e.g. a two-line macro like this:

(ns my-joxa-lists
  (require erlang))

(defmacro+ list? (l)
  `(erlang/is_list ~l))



> One last question about my setup:
>
> 5. I think I might have missed/messed something.  The joxa documentation has examples like:
>
>   joxa-is> (joxa-core/!= 1 2)
>   :true
>
>   joxa-is> (joxa-core/!= 1 1)
>   :false
>
> But in my shell I get:
>
>   joxa-is> (joxa-core/!= 1 2)
>   :0:0 *error* invalid reference joxa-core/!=/2
>   error
>
>   joxa-is> (joxa-core/!= 1 1)
>   :0:0 *error* invalid reference joxa-core/!=/2
>   error

(require joxa-core) ;)



> Any help on any of the above much appreciated.  When I finish chapter one, I'll write a blog post :D

Cool. Please tell us here when you've done so!


Cheers,
Klaus

Ivan Uemlianin

unread,
Apr 7, 2013, 4:16:08 AM4/7/13
to jo...@googlegroups.com
Dear Klaus

Thanks very much for your help. I think that clears up just about
everything, and with your explanations I understand the language a bit
more: I like the idea of a very small language (which can after all
draw on erlang libraries).

I've tested out your answers and I am ready to finish chapter 1 and
write up (probably not before next weekend!)

Some minor points:

1. reloader.

That sounds handy. I use reloader with erlang. Unfortunately for joxa
I just downloaded the escript. Don't know why I did that. I'll git
clone the source properly and try it.

2. Is there a way to quote arbitrary stuff?

Following the text of The Little Schemer I was wondering if I'd be able
to quote

(atom turkey) or

i.e., the whole of "(atom turkey) or". I don't know if you can do that
in any lisp --- afaik quote quotes a single term (the above is not a
single term, it's a list followed by an atom). It's not especially
important to be able to do this though.

Thanks again

Ivan
> --
> You received this message because you are subscribed to the Google
> Groups "joxa" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to joxa+uns...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

iv...@llaisdy.com
www.llaisdy.com
llaisdy.wordpress.com
github.com/llaisdy
www.linkedin.com/in/ivanuemlianin

festina lente
============================================================

Klaus Trainer

unread,
Apr 7, 2013, 7:52:50 AM4/7/13
to jo...@googlegroups.com
2. Is there a way to quote arbitrary stuff?

Following the text of The Little Schemer I was wondering if I'd be able
to quote

     (atom turkey) or 

i.e., the whole of "(atom turkey) or".  I don't know if you can do that
in any lisp --- afaik quote quotes a single term (the above is not a
single term, it's a list followed by an atom).  It's not especially
important to be able to do this though.

Ah. Well, it's possible to say '((atom turkey) or) and everything in the list will be quoted, but it's not possible to quote that without either putting it in a list and quoting the whole list, or quoting that things individually.
 

Btw., one thing I could have mentioned earlier which comes handy when working with the Joxa shell:

Get the rlwrap (readline wrapper) tool if you don't have it installed, and then invoke the Joxa shell via rlwrap:

    rlwrap joxa

You can also create an accordant alias by e.g. putting the following line in your ~/.bash_aliases for instance:

    alias joxa="rlwrap joxa"

With that you now have shell history; and more: reverse-i-search via Ctrl-R, and in case you have configured your shell e.g. in vi-mode, you'll also have that.

Ivan Uemlianin

unread,
Apr 7, 2013, 9:04:17 AM4/7/13
to jo...@googlegroups.com
Good idea. I keep meaning to install rlwrap. I'll do it now.

Ivan

On 07/04/2013 12:52, Klaus Trainer wrote:
> ...
> Get the rlwrap(readline wrapper) tool if you don't have it installed,
> and then invoke the Joxa shell via rlwrap:
>
> rlwrap joxa
>
> You can also create an accordant alias by e.g. putting the following
> line in your ~/.bash_aliasesfor instance:
>
> alias joxa="rlwrap joxa"
>
> With that you now have shell history; and more: reverse-i-search via
> Ctrl-R, and in case you have configured your shell e.g. in vi-mode,
> you'll also have that.



Reply all
Reply to author
Forward
0 new messages