Skip to content

Commit f906886

Browse files
committed
add flix
1 parent 3dd2762 commit f906886

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

flix/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
flix.jar

flix/justfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# just displays the receipes
3+
default:
4+
@just --list
5+
6+
# runs the project
7+
run:
8+
java -jar flix.jar run

flix/src/Algebraic Data Types.flix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/// An algebraic data type for shapes.
3+
enum Shape {
4+
case Circle(Int32), // circle radius
5+
case Square(Int32), // side length
6+
case Rectangle(Int32, Int32) // height and width
7+
}
8+
9+
/// Computes the area of the given shape using
10+
/// pattern matching and basic arithmetic.
11+
def area(s: Shape): Int32 = match s {
12+
case Shape.Circle(r) => 3 * (r * r)
13+
case Shape.Square(w) => w * w
14+
case Shape.Rectangle(h, w) => h * w
15+
}
16+
17+
// Computes the area of a 2 by 4.
18+
// def main(): Unit \ IO =
19+
// area(Shape.Rectangle(2, 4)) |> println

flix/src/High Order Functions.flix

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Returns the sum of `x` and `y`.
2+
def add(x: Int32, y: Int32): Int32 = x + y
3+
4+
/// Returns `x` plus one.
5+
def inc(x: Int32): Int32 = add(x, 1)
6+
7+
/// Returns a function that applies `f` twice.
8+
def twice(f: Int32 -> Int32): Int32 -> Int32 = x -> f(f(x))
9+
10+
/// Returns `x` plus two.
11+
def two(x: Int32): Int32 = twice(inc)(x)
12+
13+
/// Returns 123 plus 4 = 127.
14+
// def main(): Unit \ IO =
15+
// println(twice(two)(123))

flix/src/Lists.flix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// We can easily construct and append lists:
2+
def l(): List[Int32] =
3+
let l1 = 1 :: 2 :: 3 :: Nil;
4+
let l2 = 4 :: 5 :: 6 :: Nil;
5+
l1 ::: l2
6+
7+
/// We can use pattern matching to take a list apart:
8+
def length(l: List[a]): Int32 = match l {
9+
case Nil => 0
10+
case _ :: xs => 1 + length(xs)
11+
}
12+
13+
/// The Flix library has extensive support for lists:
14+
// def main(): Unit \ IO =
15+
// let l1 = l();
16+
// let l2 = List.intersperse(42, l1);
17+
// let l3 = List.map(x -> x :: x :: Nil, l2);
18+
// let l4 = List.flatten(l3);
19+
// println(l4);
20+
// println(length(l4))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// An algebraic data type for binary trees with leaves.
2+
enum Tree[a] {
3+
case Leaf(a),
4+
case Node(Tree[a], Tree[a])
5+
}
6+
7+
/// A higher-order function that transforms a tree with
8+
/// elements of type `a` to a tree with elements of type `b`.
9+
def map(f: a -> b, t: Tree[a]): Tree[b] = match t {
10+
case Tree.Leaf(x) => Tree.Leaf(f(x))
11+
case Tree.Node(l, r) => Tree.Node(map(f, l), map(f, r))
12+
}
13+
14+
/// A function that computes the sum of all leaves in a tree.
15+
def sum(t: Tree[Int32]): Int32 = match t {
16+
case Tree.Leaf(x) => x
17+
case Tree.Node(l, r) => sum(l) + sum(r)
18+
}
19+
20+
// def main(): Unit \ IO =
21+
// let t1 = Tree.Node(Tree.Leaf("Hello"), Tree.Leaf("World"));
22+
// let t2 = map(String.length, t1);
23+
// println(sum(t2))

0 commit comments

Comments
 (0)