24

After reading Practical Common Lisp I finally understood what the big deal about macros was, and I have been looking for a language for the .NET platform that supports this. There are a few lisp dialects for .NET but from what I have been able to gather all are either very beta or abandoned. Recently my interest has been sparked by Clojure, but it's for the java platform and while on probably could use ikvm it doesn't feel some integrated. Especially when you want to do stuff like WPF.

Recently I have been hearing whisper about F#, I tried to look at the documentation if I could find anything about macro support, but haven't found it. So does anyone know?

Thanks :)

Jason Olson
  • 3,616
  • 2
  • 21
  • 24
Anders Rune Jensen
  • 3,758
  • 2
  • 42
  • 53
  • 1
    Microsoft has a Scheme dialect that is used internally by the O365 team and has macro expansions, tail call optimization, etc. [here](https://github.com/microsoft/schemy) You can use the nuget package or just compile into your program from source (it's very small). Works well and provides a good scripting facility. – sean_m Aug 18 '19 at 21:34

11 Answers11

13

Well, F# is based on OCaml and OCaml has a rather extensive macro system. Given the syntactic and semantic similarities of F# and OCaml you may be able to port over the Ocaml macro system to F#.

Other than stealing Ocaml's macro system I'm unaware of a canned macro system for F#.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Jason Dagit
  • 13,684
  • 8
  • 33
  • 56
  • 3
    Porting camlp4 is not a trivial task. But it is not required as long as you are staying in ocaml-compatible subset of F#. Though the whole process will require some manual setup and lead to certain constraints. – ygrek Nov 26 '09 at 07:49
13

Nemerle, at http://nemerle.org/ , is a .NET language (also supporting mono) that supports a lot of of the functional programming paradigm while staying visually close to C#. It has extensive macro support.

ben
  • 1,994
  • 12
  • 20
7

Nope. No macros for F#.

Jim Deville
  • 10,632
  • 1
  • 37
  • 47
  • One could choose to consider computation expressions as a replacement for a macro system, in the sense that it allows for building domain specific languages of sorts. – BitTickler Apr 07 '16 at 21:45
6

Have you looked at Boo? While Boo doesn't have macros, it has an open compiler pipeline, which is a good alternative to macros for syntactic metaprogramming.

[EDIT] As noted in the comments, Boo does have macros now.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 4
    Boo currently does have macros (this may not have been the case when you posted). – JasonTrue Jul 30 '09 at 18:25
  • Might be. More likely, it *did* have macros when I posted that, but didn't when I last checked (which was long before that post.) Doesn't matter anyway, since you can do everything you can do with macros with an open compiler and vice versa. – Jörg W Mittag Jul 30 '09 at 21:30
4

I thought I should point out that there is now a pretty active .NET/Mono port of Clojure. Clojure supports LISP style macros as is noted in the question.

As others have said, macros are not supported in F# at this point (late 2010).

Justin
  • 8,853
  • 4
  • 42
  • 42
4

but good horrors the syntax in those ocaml examples looks obscure

There you're running into the same fundamental syntactic trade-off you do with Lisp. If you want the power of lisp-like macros, you tend to either end up with lisp-like syntax for the language, or else your macro syntax looks quite different from your regular syntax... nothing wrong with either approach, just different choices

simon
  • 7,044
  • 2
  • 28
  • 30
  • 1
    Not really. OCaml's macros are complicated by the need to differentiate between different kinds of syntactic constructs (e.g. type definitions vs expressions) because they are represented by values of different static types. Mathematica has rich syntax and powerful macros at the same time, for example. – J D Sep 01 '10 at 11:56
  • While I consider your statement to be true, one deduction, few ever made from this argument is, that a good compiler for a non-lisp language should have a lisp interpreter in its compiler :) Then, you could do a macro system on top of s expressions, ignoring the special syntax of the "real" language. The closest thing to this idea is in fact the Julia language. – BitTickler Nov 04 '20 at 19:46
3

Recently I have been hearing whisper about F#, I tried to look at the documentation if I could find anything about macro support, but haven't found it. So does anyone know?

F# does not support macros and it is unlikely that it ever will.

J D
  • 48,105
  • 13
  • 171
  • 274
2

How about using F# quotations?

http://tomasp.net/blog/fsquotations.aspx

Tuomas Hietanen
  • 4,650
  • 2
  • 35
  • 43
  • 2
    Quotations just let you quote code to get a runtime representation of it, and F# quotations often don't even let you do that (e.g. F# refuses to quote `<@ a @>`). Macros let you do more, e.g. change the syntax of the language. There is no way to do that in F#. F# doesn't have a complete CodeDOM either so you cannot use the F# compiler programmatically. Indeed, you could say that F# is specifically designed to prevent this kind of programming. – J D Sep 01 '10 at 13:02
1

That may be the other way around than what you want, but do you know about RDNZL? It's a foerign-function interface (FFI) that lets you call .NET libraries from your Lisp code.

They are most probably much less mature than any Common Lisp or Scheme implementation, but there are Lisp dialects for .NET: L# and DotLisp.

Nowhere man
  • 5,007
  • 3
  • 28
  • 44
  • 1
    Just FYI Dotlist isn't being maintained anymore (Rich is the guy behind Clojure) so I don't think I'd mess with it. Mind if you want a lisp-like that isn't CL/Scheme I'd use Clojure, personally. – Runevault Oct 03 '08 at 05:42
  • DotLisp is admittedly a little stunted, but that's because it's stable, working, and I'm too busy to upload my patches :-( – Mark Hurd Jun 01 '10 at 14:57
1

There are two actively developed Lisps for .net

IronScheme - DLR based scheme implementation

Xronos - DLR based port of clojure

Stefan Rusek
  • 4,737
  • 2
  • 28
  • 25
  • Xronos looks dead to me. Check out ClojureCLR - https://github.com/richhickey/clojure-clr/wiki/ – Justin Nov 07 '10 at 01:11
1

I'm currently investigating possibilities of meta-programming in F#. If we define macros as text template which expands into code then there are two obvious approaches:

  1. T4 templates. There is implementation for F#: https://github.com/kerams/Templatus

  2. I've seen somewhere invocation of F# from strings into separate assembly and then loading of the assembly.

alehro
  • 2,198
  • 2
  • 25
  • 41