0

如果我想检查输入以便立即获取最后一个元素,是否可以使用正则表达式:

有趣的 someFunction (firstElement::MiddleOfList::LastElement)

所以我可以得到最后一个元素

4

2 回答 2

1

You cannot pattern match to find the last or middle element in a SML list.

A SML list is a linked list, which means if you want to find element x, you must first visit all the elements before it.

You could however use the library functions to find the middle element in a manner similar to this:

fun someFunction list = 
   let
      val (first, middle, last) = (hd list, List.nth (list, (length list) div 2), List.last list)
   in
      (* your code here *)
   end

This will however take time linear to the length of the list, but there is no asymtotically faster way using lists, if you need constant time access, you should consider using arrays.

于 2011-02-11T15:02:57.520 回答
1

结构中似乎有一个last列表功能List;那是你需要的吗?

于 2011-02-10T03:40:21.247 回答