[go: up one dir, main page]

Skip to content
/ hm Public

🤔 is a simplistic functional programming language inspired by OCaml and Haskell

License

Notifications You must be signed in to change notification settings

kharvd/hm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤔

🤔 (pronounced "hmm") is a simplistic functional programming language inspired by OCaml and Haskell.

Disclaimer: this is a toy project and not intended for production use.

Usage

Run REPL:

$ cargo run

Features

  • Hindley-Milner type inference
  • Fixed-point combinator fix
  • Pattern matching
  • Algebraic data types
  • Lists and tuples
  • First-class functions
  • Tail call optimization
  • Whitespace insensitive
  • REPL

Examples

See prelude for more examples.

Recursion and pattern matching

let rec fib n =
  match n with
  | 0 -> 0
  | 1 -> 1
  | n -> fib (n - 1) + fib (n - 2)

FizzBuzz

putStrLn is a built-in impure function that prints a string to stdout.

let fizzbuzz n =
    let fz = "Fizz"
    in let bz = "Buzz"
    in let fzbz = "FizzBuzz"
    in let fb n =
        match (n % 3, n % 5) with
        | (0, 0) -> putStrLn fzbz
        | (0, _) -> putStrLn fz
        | (_, 0) -> putStrLn bz
        | _ -> putStrLn (intToString n)
    in
        discard (map (fun n -> fb (n + 1)) (range n))

Algebraic data types

data Maybe a = Just a | Nothing

let map f xs =
    match xs with
    | Just x -> Just (f x)
    | Nothing -> Nothing

Lists

let rec sum xs =
    match xs with
    | Nil -> 0
    | Cons x xs -> x + sum xs

let s = sum [1, 2, 3, 4, 5]

TODO

  • Standalone scripts
  • Proper prelude
  • Explicit type annotations
  • let rec .. in .. syntax sugar
  • match guards
  • Mutually recursive functions (currently supported with explicit fix)
  • Multi-argument lambdas
  • let bindings with arguments
  • Better error messages
  • Prettier pretty-printer
  • Exhaustiveness checking for pattern matching
  • String interning for identifiers
  • Compilation (native and/or to JS)
  • Typeclasses
  • ...

About

🤔 is a simplistic functional programming language inspired by OCaml and Haskell

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages