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 :)

1
  • 1
    Microsoft has a Scheme dialect that is used internally by the O365 team and has macro expansions, tail call optimization, etc. here 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, 2019 at 21:34

11 Answers 11

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#.

1
  • 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, 2009 at 7: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.

7

Nope. No macros for F#.

1
  • 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 7, 2016 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.

2
  • 4
    Boo currently does have macros (this may not have been the case when you posted).
    – JasonTrue
    Jul 30, 2009 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. Jul 30, 2009 at 21:30
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

2
  • 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 1, 2010 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 4, 2020 at 19:46
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).

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.

2

How about using F# quotations?

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

1
  • 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 1, 2010 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.

2
  • 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 3, 2008 at 5: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 1, 2010 at 14:57
1

There are two actively developed Lisps for .net

IronScheme - DLR based scheme implementation

Xronos - DLR based port of clojure

1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.