David Farthing
David Farthing

Reputation: 247

inserting EQUALOP error in SML

I am trying to swap elements in a list in ML. My swap function returns the error inserting EQUALOP.

fun swap(n:int, i:int, deck:card list) =
                local
                    val card1_removed = nth(deck,i)
                    val card2_removed = nth(deck,n)
                in
                    val deck = remove(deck,i)
                    val deck = remove(deck,n)
                    val deck = insert_at(deck,n,card1_removed)
                    val deck = insert_at(deck,i,card2_removed)
                    print_cards(deck);
                end;

Any suggestions?

Upvotes: 0

Views: 2013

Answers (1)

Jesper.Reenberg
Jesper.Reenberg

Reputation: 5944

There are a few problems with your code.

First off you cant have a local declaration inside a function definition like that. The body of a function must be an expression, and local ... in .. end is a declaration. In this case you have to use let ... in .. end, which is an expression.

Note that you can't have value declarations in the in ... end part of a let-expression though. Here you will have to move all the value declarations up in the let ... in part.

To be a bit more clear the form of let and local is:

<atexp> ::= let <dec> in <exp_1> ; ... ; <exp_n> end 

<dec>   ::= local <dec_1> in <dec_2> end

Thus, normally local is used like this

local 
  fun foo ...
  val ....
in 
  fun swap ...
end

where let is used like this

fun swap ...
  let
    val ...
  in
    ..
  end

Upvotes: 1

Related Questions