Hacker News new | past | comments | ask | show | jobs | submit login

" I also like the idea of an extensible grammar (and syntax too, right?)"

Yes, that's what I meant (you change the grammar, resulting in new syntax).

Some examples of what you can do with camlp4:

http://martin.jambon.free.fr/pa_memo.ml allows to define memoized functions very conveniently:

    (* normal *) 
    let fib = function 0 | 1 -> 1 | n -> fib (n-1) + fib (n-2)
    (* memoized *)
    let fib = memo 0 | 1 -> 1 | n -> fib (n-1) + fib (n-2)
Automatic generation of

* typed JSON marshallers (http://martin.jambon.free.fr/json-static.html)

    type json mytype = Foo | Bar of int * int
    (* just add "json" to the type declaration to create to
       create the json_of_mytype and mytype_of_json functions *)
* serialization with S-expressions (http://www.janestcapital.com/ocaml/)

* pretty-printing, type-safe marshalling with structure-sharing, dynamic typing, equality... (http://code.google.com/p/deriving/)

* list comprehensions, heredocs, string interpolation, lazy pattern matching, "do syntax" for monads (very much like Haskell's)...

Here's some OCaml code that relies on a rather large syntax extension of mine which allows you to generate (or verify) SQL schemas automatically and build composable queries using a typed relational algebra (the type system ensures that all queries are valid; if you change the schema and break some queries, the compiler will tell you what's wrong --- broken queries just don't compile):

   TABLE user users
     COLUMN id SERIAL AUTO PRIMARY KEY
     COLUMN name VARCHAR(64) UNIQUE
     COLUMN age INT NULLABLE INDEXED
     COLUMN password VARCHAR(64)
   END
   
   TABLE comment comments
     COLUMN id SERIAL AUTO PRIMARY KEY
     COLUMN title TEXT
     COLUMN text TEXT
     COLUMN created_at TIMESTAMPZ
     COLUMN author SERIAL FOREIGN(users, id)
   END
   
   let minors x = SELECT [User_age < (Some 18)] x
   let pauls = SELECT [User_name LIKE "%Paul%"] users
   let young_pauls = minors pauls
You can read more about this extension at http://eigenclass.org/hiki/typed-relational-algebra-in-OCaml



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: