JCaml


About JCaml

After taking a programming languages class, we became intrigued with functional programming via OCaml. While we loved OCaml, we realized that it seemed to be limiting itself as a truly modern programming tool. It had so much power, yet it seemed to fall short. So much of today's world is done on the web that JavaScript has become one of the most popular languages in the world. It only made sense to harness the power of OCaml and combine it with the ubiquity of JavaScript.

JCaml is statically-typed and statically-scoped, it has pattern-matching, dictionary capabilities, list comprehension, recursive functions, higher-order functions, currying, and tuples. It is designed to be a functional programming language for the modern era, with browser compatibility through JavaScript compilation.


Examples

Hello World!

JCaml

spit("Hello, World");;

JavaScript

console.log("Hello World");

Fibonacci Numbers

JCaml

let fun fib = (int a) => (int):
  if(a == 0 or a == 1):
    hump 1
  else:
    hump fib(a-1) + fib(1-2)
;;

JavaScript

let fib = (a) => {
  if (a == 0 || a == 1) {
    return 1;
  } else {
    return fib(a - 1) + fib(a - 2);
  }
};

Fibonacci Numbers Memoized

JCaml

let fibDict = []
let fun fibMem = (int a) => (int):
  if (a == 0 or a == 1):
    hump 1
  else if (a in fibDict):
    hump fibDict[a]
  else:
    fibDict[a] = fibMem(a-1) + fibMem(a-2)
    hump fibDict[a]
;;

JavaScript

let dictionary = {};
let fibMem = (a) => {
  if(a == 0 || a == 1) {
    return 1;
  } else if(a in dictionary) {
    return dictionary[a];
  } else {
    dictionary[a] = fibMem(a - 1) + fibMem(a - 2);
    return dictionary[a];
  }
};

Currying and Higher Order Functions

JCaml

let fun add = (int a, int b) => (int):
  hump a + b;
;;
let fun add2 = add(2);;

JavaScript

let add = (a) => {
  return (b) => {
    return a + b;
  };
};
let add2 = add(2);

Match Statement

JCaml

let fun count_occurences = (int v, int list l) => (int):
  match l with:
  | [] -> 0
  | hd::[] -> hump hd == v ? 1 : 0
  | hd::tl -> hump hd == v ? 1 + count_occurences(v, tl) ; count_occurences(v, tl)
;;

JavaScript

let count_occurences = (v, l) => {
let sum = 0;
for (let value of l) {
  if(v === value) {
    sum++;
  }
}
  return sum;
};

Syntax Diagrams

Program
Block
Stmt
Decl
FuncDec
FuncCall
Arg
Args
Params
Param
Return Type
Body
Exp
MatchExp
BinExp
AddExp
MullExp
PrefixExp
ExpoExp
ParenExp
Matches
keyword
prefixop
id
Tuplit
List
Print
idrest
relop
addop
mullop
expop
binop
Numlit
char
escape
Charlit
Stringlit
comment


Copyright © JCaml 2017