Search code examples
listsmlsmlnj

How to shorten a list in standard ml


I just start working with standard ml and really have some trouble understanding the list in this language. So my question is how to shorten a list in ml ? For example, if I have a list [1,2,3,4,5,6], I want to shorten it to [1,2]. What I'm having so far is:

fun shorten(i, l) = let val newlen = i in newlen = length l//in correct

what I want is a function that will take i as a location that user want to shorten the list and l is the list. In this case, the input should look like shorten(2, [1,2,3,4,5,6] and the output should look like [1,2]


Solution

  • This function should do it:

    fun shorten(_, nil) = nil
      | shorten(0, _) = nil
      | shorten(i, x::xs) = x::shorten(i - 1, xs)
    

    As you noticed, this function doesn't throw any exceptions when i is larger than the length of the list. An approach that uses exceptions would be this:

    exception IllegalArgument
    
    fun shorten(_, nil) = nil
      | shorten(0, _) = nil
      | shorten(i, x::xs) =
        if i > length(x::xs) then raise IllegalArgument
        else x::shorten(i - 1, xs)
    

    In SML you need to declare any exception type with exception before it is raised using raise. Otherwise, the exception type is not bound in the environment and the interpreter will complain about the symbol being unknown.