# Comparison to scheme

**URL:** <https://discuss.ocaml.org/t/comparison-to-scheme/9893>\
**Category:** Learning\
**Created:** [May 21, 2022, 4:27pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893 "2022-05-21T16:27:31Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![devosalain](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/devosalain/32/3325_2.png) [@devosalain](https://discuss.ocaml.org/u/devosalain)\
**Post date:** [May 21, 2022, 4:27pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/1 "2022-05-21T16:27:31Z")

</div>

Does scheme/racket has essential features which are missing in ocaml?  
Or which can be re-implemented in ocaml ?

---

<div class="post-metadata">

**Author:** ![hyphenrf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hyphenrf/32/2733_2.png) [@hyphenrf](https://discuss.ocaml.org/u/hyphenrf)\
**Post date:** [May 22, 2022, 7:17am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/2 "2022-05-22T07:17:23Z")

</div>

I’d argue they’re very different languages when it comes to the feature set. As in comparing them would be apples-to-oranges kind of situation.  
If so, a more productive question may be what features from Racket (or other schemes) one would like to have in OCaml… Not just _whether_ features are missing.

I haven’t used Racket extensively enough to be qualified to answer this…  
Instead I’ll share a “feature” that’s not part of the language, but from my little exposures to their ecosystem: I’d love to have a docs site as good as [theirs](https://docs.racket-lang.org/reference/cont.html#%28form._%28%28lib._racket%2Fcontrol..rkt%29._prompt%29%29).

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [May 22, 2022, 9:39am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/3 "2022-05-22T09:39:38Z")

</div>

Is this a purely academic question or used to decide which language to use? As a starting point, two differences I would name:

- Difficult to emulate in Scheme: static typing and the module system
- Difficult to emulate in OCaml: Scheme macros

---

<div class="post-metadata">

**Author:** ![devosalain](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/devosalain/32/3325_2.png) [@devosalain](https://discuss.ocaml.org/u/devosalain)\
**Post date:** [May 22, 2022, 1:22pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/4 "2022-05-22T13:22:51Z")

</div>

The question was posed in large context, but i wanted to digest which language is best fit for which kind of problems.

---

<div class="post-metadata">

**Author:** ![Gopiandcode](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/gopiandcode/32/5346_2.png) [@Gopiandcode](https://discuss.ocaml.org/u/Gopiandcode)\
**Post date:** [May 23, 2022, 10:00am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/5 "2022-05-23T10:00:05Z")

</div>

One word: macros.

Or to be more precise, an easy to use, easy to check macro system. The current sota w.r.t meta programming in OCaml is through ppx extensions, but these require directly manipulating OCaml’s AST data structure, which can lead to macros that are hard to understand and debug.

---

<div class="post-metadata">

**Author:** ![zhtprog](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zhtprog](https://discuss.ocaml.org/u/zhtprog)\
**Post date:** [May 24, 2022, 6:45am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/6 "2022-05-24T06:45:36Z")

</div>

Scheme/Lisp is great if you need to embed an interpreter so that the behavior of your program can be modified at runtime. A good example is the Emacs editor which exposes its programmability through its own version of Lisp. As someone famous once said to the tune that any program with complex enough behavior probably has an embedded Lisp interpreter running somewhere, potentially buggy if the programmer hasn’t realized that is the case!

OCaml’s power is mostly in statically type checking your program so that your program is more likely to run correctly. You can write large programs with more confidence. Your program is also easier to maintain as the types don’t lie but the API documents or comments (if they exist at all) could. The code generated is quite fast and the compiler is really fast. You get more help from your LSP while writing your code than almost anything else.

---

<div class="post-metadata">

**Author:** ![zhtprog](https://avatars.discourse-cdn.com/v4/letter/z/58f4c7/32.png) [@zhtprog](https://discuss.ocaml.org/u/zhtprog)\
**Post date:** [May 24, 2022, 6:59am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/7 "2022-05-24T06:59:56Z")

</div>

Incidentally does anyone know if there is any way to embed Lisp/Scheme in OCaml? Standard embedding of an external implementation seems challenging due to dueling garbage collectors. Probably best achieved through IPC in which case one should probably just go with the modern Lisp (I mean Javascript 😄 ). Does anyone do IPC between utop and native compiled code? Data marshaling would be much easier with utop, no?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [May 24, 2022, 10:27am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/8 "2022-05-24T10:27:44Z")

</div>

> [@zhtprog](#):
>
> Incidentally does anyone know if there is any way to embed Lisp/Scheme in OCaml?

Not exactly what you are after but you can embed Lua via [lua-ml](https://github.com/lindig/lua-ml). The [accompanying paper](https://www.cs.tufts.edu/~nr/pubs/embedj.pdf) shows a few technique on how to embed a language in OCaml.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [May 24, 2022, 12:34pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/9 "2022-05-24T12:34:30Z")

</div>

> [@zhtprog](#):
>
> Incidentally does anyone know if there is any way to embed Lisp/Scheme in OCaml?

Note that there are pure OCaml implementations such as [GitHub - freehck/ocs: OCS: a mirror of the ocaml OCS scheme interpreter](https://github.com/freehck/ocs) .

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [May 25, 2022, 9:58am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/10 "2022-05-25T09:58:06Z")

</div>

Keep in mind that this is an implementation of Lua 2.5, not quite the same as the 5.1/5.2/LuaJIT/5.3 language that people use these days. But for scripting it might be sufficient.

---

<div class="post-metadata">

**Author:** ![Leonidas](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/leonidas/32/4039_2.png) [@Leonidas](https://discuss.ocaml.org/u/Leonidas)\
**Post date:** [May 25, 2022, 10:08am UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/11 "2022-05-25T10:08:45Z")

</div>

> [@devosalain](#):
>
> Does scheme/racket has essential features which are missing in ocaml?

The main important selling point I would say is hygienic macros, as mentioned by @Gopiandcode. While the situation has improved much from Camlp4, Camlp5, omp and now ppxlib, the language itself doesn’t really have support for defining new syntax.

The issue at hand is two-fold: Scheme’s syntax is essentially just (valid) S-expression that have an attached semantic to them. So you can define your own interpretations of these s-expressions, to expand into simpler s-expressions that eventually expand in special-forms. OCaml doesn’t have a structure like this, the AST is not accessible to the regular user and it is typed so you can only represent what the authors of OCaml have forseen.

Then the other thing is that the macro-system is integrated in the language, a macro is essentially a function of an s-expression to and s-expression (`syntax-rules` even implements a system that is reminiscent of pattern patching). Similarly like functors can be thought of as functions from modules to modules, integrated into the language. Whereas OCaml PPX macros work by running the OCaml program through a program which outputs another program. This works similarly, but it is not as integrated (in a way, how people implement Go generics by using external code generators).

The system is a bit more involved than that because OCaml has a few useful additions for PPXes but I would say a macro system like Scheme just can’t be implemented in OCaml, for better or worse.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [May 25, 2022, 7:25pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/12 "2022-05-25T19:25:01Z")

</div>

I keep wondering about this. Theoretically, shouldn’t it be possible for the compiler to provide a quote/unquote macro mechanism hides the raw AST transformations? Rust, Crystal, and other typed languages seem to do that.

---

<div class="post-metadata">

**Author:** ![jjb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jjb/32/2713_2.png) [@jjb](https://discuss.ocaml.org/u/jjb)\
**Post date:** [May 25, 2022, 7:51pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/13 "2022-05-25T19:51:52Z")

</div>

This sort of conversation, that might be over-simplified to “can we have schemesque macros”, always makes me think “Doesn’t MetaOCaml already do better?”. So, genuine question: am I confused, or is there something missing, or…?

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [May 25, 2022, 8:45pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/14 "2022-05-25T20:45:26Z")

</div>

Perhaps I’m missing something. Is anyone using MetaOCaml in production in industry? It seems like the compiler team did not wish to pursue that direction and chose to go with PPX instead?

---

<div class="post-metadata">

**Author:** ![ivg](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ivg/32/103_2.png) [@ivg](https://discuss.ocaml.org/u/ivg)\
**Post date:** [May 25, 2022, 9:10pm UTC](https://discuss.ocaml.org/t/comparison-to-scheme/9893/15 "2022-05-25T21:10:50Z")

</div>

MetaOCaml serves completely different purposes. Unlike general macro-processors and syntax-rewriters, which are mostly used for reducing the boilerplate in cases when the other language constructs are not expressive enough, MetaOCaml’s purpose is runtime code optimization. One of the most important distinctions is that the MetaOCaml interface is purely generative, i.e., it only lets you generate the code values, but never inspect them. Therefore, we can’t really say that MetaOCaml is a macro-processor.

> [@yawaramin](#):
>
> It seems like the compiler team did not wish to pursue that direction and chose to go with PPX instead?

As noted above, those features solve completely different tasks. What could be solved with PPX couldn’t be solved with MetaOCaml and vice verse. MetaOCaml is about multi-staged code optimizations. And while it is possible to do some of them using staged compuation features of Lisps, it is totally possible to implement them without any macro-processing. For example, [Refal](https://en.wikipedia.org/wiki/Refal) has a supercompiler and does staged computations without any macro-processing. It even lacks the macro system, despite that it is monoiconic and is basically a variant of Lisp with trees instead of lists.
