Standard ML (Moscow) question.

I’ve elected to take a course in functional programming that uses SML (Moscow ML). So far, we’ve mostly been using the interactive top level, but now I need to write an assignment in an .sml file for submission.

The problem is, whenever I write a val statement, and load my file into the interpreter, it baulks. For instance, this is my file:



fun soundex name =
	val list = String.explode name;
	val firstCharacter = List.nth (list, 0);
	val listWithoutFirst = List.drop (list, 1);


Loading that into mosml using use “soundex.sml” produces the following error:



[opening file "soundex.sml"]
File "soundex.sml", line 2, characters 1-4:
!       val list = String.explode name;
!       ^^^
! Syntax error.
[closing file "soundex.sml"]


What am I doing wrong? Using the same val statements in the top level (without the function surrounding them) works fine.

Thanks.

Two things to try: (1) I don’t think you want those semi-colons in any code meant for compilation (only in the interactive interpreter), and (2) although the compiler didn’t complain about it yet, your function doesn’t compute anything. I think you want:



fun soundex name =
  let
    val list = String.explode name
    val firstCharacter = List.nth (list, 0)
    val listWithoutFirst = List.drop (list, 1)
  in
    [ Compute or do something here. ]
  end


I haven’t tested this however.

Belting. Worked great, thanks.