Functional programming using SML/NJ
March 9, 2024 Leave a comment
Learning to code an older FP language like Lisp or SML may seem hard at first. But gradually one realises there are SML resources and books that deal with deeper areas of CS like theorem provers and algorithms. In fact there are more SML resources than OCaml resources even though some popular tools are coded in OCaml which is relatively new.
So I started collecting some basic SML ideas that are useful when one encounters a course website or a tool that is coded in SML/NJ. It is a work in progress.
The first aspect to consider is the IDE. Mine is now Doom Emacs. Why ?
The IDE helps me focus on the task at hand. I don’t have to stray anywhere while I code. I use its eshell , DIRED facility and org mode.
Build configuration
group
(* CM allows you to selectively export defined modules (structures,
signatures and functors) by listing them here. It's useful for
libraries. *)
source (-) (* export all defined modules *)
structure Main (* OR, export selectively *)
signature FOO
functor Foo
structure Test(* OR, export selectively *)
signature THEOREM
functor Theorem
is
(* Import the SML standard library, aka Basis. *)
(* See: http://sml-family.org/Basis/ *)
$/basis.cm
(* Import the SML/NJ library *)
(* Provides extra data structures and algorithms. *)
(* See: https://www.smlnj.org/doc/smlnj-lib/Manual/toc.html *)
$/smlnj-lib.cm
(* List each source file you want to be considered for compilation. *)
./main.sml
./foo.sig
./foo.fun
./test.sml
./theorem.sig
./theorem.fun
The main purpose is a straightforward build and test environment. I don’t code SML but port it to Racket or OCaml. I copy pieces of code and test them. So here a _Functor_ is used only for convenience. There is no attempt to abstract anything.
And I will strive to add more details as I learn.