[sml] add old sml files

This commit is contained in:
Julin S 2023-04-08 14:18:37 +05:30
parent 7218c99d19
commit 0b5c5982ee
6 changed files with 155 additions and 0 deletions

5
.gitignore vendored
View File

@ -1,3 +1,8 @@
# Web
coq/*.html
coq/*.js
coq/*.css
# Haskell
*.hi
*.o

4
sml/README.org Normal file
View File

@ -0,0 +1,4 @@
- fibo.sml: Fibonacci numbers
- reverse.sml: Reverse a list
- tree.sml: Representing a tree as a type
- phoas.sml: PHOAS example

19
sml/fibo.sml Normal file
View File

@ -0,0 +1,19 @@
(* Fibonacci series:
Val: 1 1 2 3 5 8 13 21 34 55 89
Idx: 0 1 2 3 4 5 6 7 8 9 10
*)
fun fibo n =
let
fun aux a b 0 = a
| aux a b n = aux b (a+b) (n-1)
in
aux 1 1 n
end
(*
sml> fibo 10;
val it = 89 : int
*)

18
sml/phoas.sml Normal file
View File

@ -0,0 +1,18 @@
datatype 'v tm
= Var of 'v
| App of 'v tm * 'v tm
| Abs of 'v -> 'v tm
val a = Abs (fn x=>App (Var x, Var x))
val b = App (a, a)
(* numVars : () tm -> int *)
fun numVars t =
case t of
Var v => 1
| App (t1, t2) => (numVars t1) + (numVars t2)
| Abs f => numVars (f ())
(* - numVars b; *)
(* val it = 4 : int *)

15
sml/reverse.sml Normal file
View File

@ -0,0 +1,15 @@
(* Reverse a list in O(n) time *)
fun rev [] = []
| rev (x::xs) =
let
fun aux_rev [] rev = rev
| aux_rev (x::xs) rev = aux_rev xs (x::rev)
in
aux_rev xs [x]
end
(*
sml> rev [3,2,1,0];
val it = [0,1,2,3] : int list
*)

94
sml/tree.sml Normal file
View File

@ -0,0 +1,94 @@
(*
10
| \
5 15
|\ | \
2 6 16 20
5
|\
2 10
/\
6 15
| \
16 20
Node(Node(Null, 2, Null), 5, Node( Node(Null, 6, Null), 10, Node(Node(Null, 16, Null), 15, Node(Null, 20, Null))))
Node(
Node(Empty, 2, Empty),
5,
Node(
Node(Empty, 6, Empty),
10,
Node(
Node(Empty, 16, Empty),
15,
Node(Empty, 20, Empty),
),
)
)
Node(
Node(
Node(Empty,2,Empty),
5,
Node(Empty,6,Empty)
),
10,
Node(
Node(Empty,16,Empty),
15,
Node(Empty,20,Empty)
)
)
*)
datatype 'a Tree = Empty
| Node of 'a Tree * 'a * 'a Tree
(*
treemap :: ('a -> 'b) -> 'a Tree -> 'b Tree
*)
fun treemap f Empty = Empty
| treemap f (Node (l, x, r)) = Node (treemap f l, f x, treemap f r)
(*
λ> treemap (fn x => x+1) (Node (Empty, 3, Empty));
val it = Node (Empty,4,Empty) : int Tree
*)
(*
inorder :: 'a Tree -> 'a list
*)
fun preorder Empty = []
| preorder (Node (l, x, r)) = [x] @ (preorder l) @ (preorder r)
(*
λ> preorder(Node (Empty, 3, Empty))
val it = [3] : int list
λ> preorder (Node( Node( Node(Empty,2,Empty), 5, Node(Empty,6,Empty)), 10, Node( Node(Empty,16,Empty), 15, Node(Empty,20,Empty))))
= ;
val it = [10,5,2,6,15,16,20] : int list
*)
fun postorder Empty = []
| postorder (Node (l, x, r)) = (postorder l) @ (postorder r) @ [x]
(*
λ> postorder (Node( Node( Node(Empty,2,Empty), 5, Node(Empty,6,Empty)), 10, Node( Node(Empty,16,Empty), 15, Node(Empty,20,Empty))));
val it = [2,6,5,16,20,15,10] : int list
*)
fun inorder Empty = []
| inorder (Node (l, x, r)) = (inorder l) @ [x] @ (inorder r)
(*
λ> inorder (Node( Node( Node(Empty,2,Empty), 5, Node(Empty,6,Empty)), 10, Node( Node(Empty,16,Empty), 15, Node(Empty,20,Empty))));
val it = [2,5,6,10,16,15,20] : int list
*)
(*
rotClock :: 'a tree -> 'a tree
*)
fun rotClock Empty = Empty
fun rotClock (Node(Empty, x, r)) = Node(Empty, x, r)
fun rotClock (Node(Node(ll, lx, lr), x, r)) = Node(ll, lx, Node(lr, x, r))