Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions sml/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY: clean

clean:
rm exec.*
rm munchausen

smlnj:
$(MAKE) smlnj-build
$(MAKE) smlnj-run

mlton:
$(MAKE) mlton-build
$(MAKE) mlton-run

smlnj-run:
sml @SMLload=`echo exec.*`

mlton-run:
./munchausen

mlton-build:
mlton munchausen.mlb

smlnj-build:
sml smlnj.sml
23 changes: 23 additions & 0 deletions sml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# FILES:

- Makefile -- the make file
- munchausen.sml -- the main function definition
- mlton.sml -- the mlton main file
- munchausen.mlb -- the mlton build file
- smlnj.sml -- the smlnj build file
- README.md -- this file

You need either mlton or smlnj or both to run the test

# MAKE COMMANDS

`make clean` to clean

`make smlnj` to use smlnj
`make mlton` to use mlton

`make smlnj-build` to build smlnj standalone image
`make mlton-build` to build mlton executable

`make smlnj-run` to run smlnj standalone image
`make mlton-run` to run mlton executable
1 change: 1 addition & 0 deletions sml/mlton.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main () ;;
3 changes: 3 additions & 0 deletions sml/munchausen.mlb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(SML_LIB)/basis/basis.mlb
munchausen.sml
mlton.sml
35 changes: 35 additions & 0 deletions sml/munchausen.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fun main () =
let
(* utilities *)
fun compound 0 f x = x
| compound n f x = compound (n-1) f (f x)
fun expt x y = compound y (fn z => x * z) 1
fun rangeIter x y r =
if x = y then
x::r
else
rangeIter x (y - 1) (y::r)
fun range x y =
rangeIter x y []
(* main part *)
val cache = Vector.fromList (0 :: (map (fn x => expt x x) (range 1 9)))
fun cacheN x = Vector.sub (cache, x)
fun munchausenp x =
let fun iter (n, r) =
case (n > 0, r > x)
of (false, _) => r = x
| (_, true) => false
| (_, false) => iter (n div 10, r + cacheN (n mod 10))
in
iter(x div 10, cacheN (x mod 10))
end
val max = 440000000
fun loop i =
case (i < max, munchausenp i)
of (false, _) => ()
| (_, false) => loop (1 + i)
| (_, true) => (print ((Int.toString i) ^ "\n") ; loop (1 + i))
in
loop 0
end
;; (* end of main *)
5 changes: 5 additions & 0 deletions sml/smlnj.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use "munchausen.sml" ;;
SMLofNJ.exportFn ("exec", fn (_, _) => (
main () ;
OS.Process.exit OS.Process.success
))