diff --git a/samples/fpsamples/balance.hs b/samples/fpsamples/balance.hs new file mode 100644 index 0000000..1415685 --- /dev/null +++ b/samples/fpsamples/balance.hs @@ -0,0 +1,8 @@ +data Color = R | B +data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a) + +balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d) +balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d) +balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d) +balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d) +balance color a x b = T color a x b diff --git a/samples/fpsamples/sample1.erl b/samples/fpsamples/sample1.erl new file mode 100644 index 0000000..49bc5f7 --- /dev/null +++ b/samples/fpsamples/sample1.erl @@ -0,0 +1,14 @@ +% Message passing in Erlang - the "add" and "mult" +% identifiers are "atoms", constant values that are +% used to match incoming messages (which are technically +% tuples). + +loop() -> + receive + {add, A, B} -> + io:format("adding: ~p~n", [A + B]), + loop(); + {mult, A, B} -> + io:format("multiplying: ~p~n", [A * B]), + loop() + end. diff --git a/samples/fpsamples/sample2.hs b/samples/fpsamples/sample2.hs new file mode 100644 index 0000000..fbbb80f --- /dev/null +++ b/samples/fpsamples/sample2.hs @@ -0,0 +1,10 @@ +-- In Haskell, patterns are used to define function overloads. +-- This is a very "static" case. + +data Food = Pasta | Pizza | Chips + +isEqualFood :: Food -> Food -> Bool +isEqualFood Pasta Pasta = True +isEqualFood Pizza Pizza = True +isEqualFood Chips Chips = True +isEqualFood _ _ = False \ No newline at end of file diff --git a/samples/fpsamples/sample3.hs b/samples/fpsamples/sample3.hs new file mode 100644 index 0000000..7fb960e --- /dev/null +++ b/samples/fpsamples/sample3.hs @@ -0,0 +1,12 @@ +-- This function uses a boolean guard and defines +-- its value only for the remaining cases. +fact :: Int -> Int +fact x | x <= 1 = 1 + | otherwise = x * (fact (x - 1)) + +-- Alternatively, pattern matching can be used for an +-- even more explicit declaration. +fact' :: Int -> Int +fact' 0 = 1 +fact' 1 = 1 +fact' x = x * (fact (x - 1)) \ No newline at end of file diff --git a/samples/fpsamples/sample4.fs b/samples/fpsamples/sample4.fs new file mode 100644 index 0000000..1d77809 --- /dev/null +++ b/samples/fpsamples/sample4.fs @@ -0,0 +1,14 @@ +// In F# the keywoard `match` can be used to run an +// explicit pattern match. + +let rec listLength l = + match l with + | [] -> 0 + | _ :: xs -> 1 + (listLength xs) + +// Alternatively, this special syntax is available +// that removes the explicit parameter and the +// `match` call. +let rec listLength' = function + | [] -> 0 + | _ :: xs -> 1 + (listLength' xs) \ No newline at end of file