Giter Site home page Giter Site logo

thautwarm / ml-to-scheme Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 0.0 235 KB

Allow using ML to teach courses that're usually given in LISPs

License: MIT License

Python 96.06% OCaml 3.53% Racket 0.38% Shell 0.03%
racket lisp-compiler meta-language sicp

ml-to-scheme's Introduction

ml-to-scheme

A simple ML language, see grammar at https://github.com/thautwarm/ml-to-scheme/blob/master/yesml.rbnf .

Target racket.

In fact not very trivial due to the greatly reduced code, and more easy-to-write and powerful pattern matching.

Usage

After cloning this repo, how about installing dependencies and ml2scm executable in 10 seconds ?

You might have suffered a lot from npm or other slow package managers.

git clone https://github.com/thautwarm/ml-to-scheme &&
pip install rbnf-rts                                &&
python setup.py install

And the usage is easy,

ml2scm <xxx.ml> <xxx.rkt>
racket xxx.rkt

# `mlscm xxx.ml` will execute the file directly.

Grammar

Do

In top level, you can execute expressions via the do syntax.

do println 1

Let-Definition

In top level, you can define a variable in this way:

let x = 1
let add10 = fun x -> x `+` 10

Open

It'll get mapped to racket's require syntax.

open racket/match
open "./utils"

maps to

(require racket/match)
(require "./utils")

Block Expression

Concatenate a list of expressions, execute them in order and treat the last one as return.

let x = print 1;
        print 2;
        3

Let In Expression

let ... and ... in ... juxtaposes several independent local bindings, and return the expression held after the token in.

let rec ... and ... in makes this series of local bindings able to mutually reference each other.

let f = fun x ->
    let rec f = fun x -> g x
    and g = fun y -> y + 1
    in f 2

Bool Expression

true || false
a `equals?` b && c

If, Lambda, Match

If:

if cond then
    true_clause
else
    false_clause

Lambda:

fn x -> x
fn x -> x end
fun x -> x
fun x -> x end

When there're syntactic conflicts, feel free to use end to resolve.

Match:

match x with
| [3, 4.0, "str", :symbol, _] -> ...
| [hd#tl] -> ...
| pattern when predicate ->
| as-pattern -> dosomewith(as-pattern)
| pat1 & pat2 & pat3 -> ...
| pat1 | pat2 | pat3 -> ...

The precedence of & is higher than |.

Still, feel free to add an end in the end of a match expression, to resolve possible syntactic conflicts.

Application

Things are not curried in fact.

f 1 is a syntax sugar for f(1).

You can define functions in this way:

let f = fun x1 -> fun x2 -> ...

By this, you can then use the function in the currying way f 1 2 ....

List Expression

There's no Tuple, due to the target language is racket. Use lists as tuples will suffice you use cases.

Note that to construct a list with a head and a tail, you can use [hd1, hd2, hd3#tl] or cons(hd1, cons(hd2, cons(hd3, tl))).

Check test directory for more examples.

ml-to-scheme's People

Contributors

thautwarm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.