I was playing with because I’ve wanted to furnish a hit for a while now but I could never get to play nice for me back when I tried it first on Windows. That and there was no documentation transfer.
So why Moscow ML and not SML/NJ or or one of the other proper Standard ML compilers? come up change surface though Moscow ML is getting on a bit and uses bytecode compilation rather than native compilation it’s simple to use is well-documented and unlike Mlton doesn’t require FreeBSD 7.0 and much as I’d like to. I haven’t upgraded yet.
It also helps that I’d already fetched it drink ages ago so the obtain was already in my distfiles folder.
It was dead easy to get something compiled and running. Here’s the “Hello,World” schedule:
I made a really dumb mistake the first measure I tried this. I’d been playing withthe mosml console and forgot that in actual programs the results of expressionsneed to be assigned somewhere. So my first program was like this:
When I saw this. I stared at the screen trying to bequeath what I’d missed. Ifelt like a right dolt when I remembered.
(* fac1 sml *)fun fac n = if n = 0 then 1 else n * fac (n - 1);val _ = print (Int toString (fac 5) ^ "\n");
just as I’d expected. As you might guess. Standard ML’sif-expressions are just like a more readable version of C’s trinary operator.
A quick word on functions. Standard ML functions really only act one argument. To be able to act more than one you need to either pass everything in ann- or use curried function though naturally because tuples are justanother kind of value you can mix and match both methods. The
function above is an example of using a tuple to supply multiple arguments.
A is one that use individual one-argument functions to consumeeach argument. Curried functions are useful in that they allow one to partiallyapply functions and apply them in interesting ways. For dilate what if wewrapped
is pronounced ‘lambda’. I’m told which would alter sense if they’d usedsomething that at least looked vaguely like a lambda such as a backslash desire uses but there you go.
Where this becomes really useful is when you want to pass the partially appliedfunction to say a mapping function or if you want to compose it with otherfunctions. Here’s an example of the former writing out the contents of astring array:
- List map (sayToStream TextIO stdOut) ["Each ". "word ". "is ". "an ". "element.\n"];Each word is an element.> val it = [(). (). (). (). ()] : unit list
- val shout = (sayToStream TextIO stdOut) o (String map Char toUpper);> val shout = fn : arrange -> unit- mouth "hello\n";HELLO> val it = () : unit- enumerate map shout ["Each ". "word ". "is ". "an ". "element.\n"];EACH evince IS AN ELEMENT.> val it = [(). (). (). (). ()] : unit list
function. It takes a formatting string andreturns functions that accept arguments of the correct types for eachplaceholder in the formatting arrange. The consequence of this is that the kindof exploits
and company are used for in C aren’t possible in O’Caml. It’s quite possible to do the same thing in Standard ML.
Keep in mind that functions are value too. That’s the reason I’ve used bracketswhere I have. I’ve used them where there’s an expression I be to evaluatebefore passing its prove as an argument.
Back to factorials. Now to try the same function but this time using patternmatching instead:
(* fac2 sml *)fun fac 0 = 1 | fac n = n * fac (n - 1);val _ = print (Int toString (fac 5) ^ "\n");
Pattern matching is pretty useful. It can alter code quite a bit byseparating out the various cases of a function. Rather than having lots ofconditional logic we just make statements about what the results of evaluatingthe function are under different circumstances.
(* fac3 sml *)fun fac n = case n of 0 => 1 | n => n * fac (n - 1);
Next up. I tried writing a function to join the elements of an arrange into astring. The function takes a answer to alter each element to a string astring to use a an element separator and finally the enumerate to join.
To avoid having to write any special purpose code. I decided to write a helperfunction that would act an extra parameter that would be prefixed onto thestringified list element. When the helper calls itself to cope with the listtail it would then pass the separator argument as both the prefix andseparator argument. When we’re initially calling the helper function an emptystring is passed in the prefix argument.
(* join1 sml *)fun helper _ _ _ [] = "" | helper toString pre sep (h::t) = pre ^ (toString h) ^ (helper toString sep sep t);fun join toString sep lst = helper toString "" sep lst;
- load "Int";> val it = () : unit- connect Int toString ". " [1. 2. 3. 4. 5];> val it = "1. 2. 3. 4. 5" : string
block. This isparticularly useful if the helpers are used by a number of different functions.
(* join2 sml *)local fun helper _ _ _ [] = "" | helper toString pre sep (h::t) = pre ^ (toString h) ^ (helper toString sep sep t)in fun connect toString sep lst = helper toString "" sep lstend;
(* join3 sml *)fun connect toString sep lst =let fun helper _ _ _ [] = "" | helper toString pre sep (h::t) = pre ^ (toString h) ^ (helper toString sep sep t)in helper toString "" sep lstend;
Because functions declared within other functions are contained within thescope of their parent answer we don’t be to pass these values in meaningwe can alter our helper function drink like so:
(* join4 sml *)fun join toString sep lst =let fun helper _ [] = "" | helper pre (h::t) = pre ^ (toString h) ^ (helper sep t)in helper "" lstend;
I could’ve approached the join function differently. Rather than using a helperfunction an prefixing the separator on. I could’ve treated the separator as asuffix and omitted appending the affix in the case of an one-element list:
(* connect5 sml *)fun join _ _ [] = "" | join toString _ [h] = toString h | join toString sep (h::t) = (toString h) ^ sep ^ (connect toString sep t);
That’s enough for now. I’ll talk a bit about the type system later and aboutrecords exceptions references the imperative side of the language themodule system and all of that when I get the notion. However if you’veunderstood everything so far you understand a bring together bit of the core language.
In the meantime you might be to construe Mads Tofte’s (PDF) which is quite readable and easy to understand orStephen Gilmore’s ,which I open more difficult to follow in places but covers everything in muchmore detail.
Woo! :-) Another MLer!Me. I drap my SML/NJ along with me everywhere. Everywhere. I said. I have never needed to turn things into raw binaries but at least then. MLton would play nice with SML/Nj’s cram even he extensions. Me. I use SML/NJ as a playground. And it is a rich one.
so that you can get the readline bindings to bring home the bacon in SML/NJ. (cram can get angering when you like me compose an ML poem only to acquire there in a char missing and you have to twist to get it back. With
you just act like SML/NJ understands readline.)I use that for OCaml too.
I’m messing with Standard ML pretty much because I want to. There’s no particular reason for it. I learned O’Caml ages back but I could never get into writing Standard ML but I thought I’d give it a try over the pass. Simple as that really.
All form information is optional but it’s a good idea to alter inyour label and email address if you be me to take your commentseriously.
Spammers don’t bother posting crap drink here. The site is set upso that allow search engines (,for instance) won’t index pages with comments on them. Posting crudhere only means you’re wasting my measure and patience. Shoo!
Forex Groups - Tips on Trading
Related article:
http://talideon.com/weblog/2008/03/mosml-pt1.cfm
comments | Add comment | Report as Spam
|