Scala:列出操作

时间:2015-04-08 17:48:57

标签: list scala functional-programming

在Scala中,定义函数slice(from,until,xs),从(字符串)列表xs中选择元素的间隔,使得对于以下不变量的区间中的每个元素x:from <= indexOf(x) < until

from: the lowest index to include from this list.
until: the highest index to exclude from this list.

返回:包含大于或等于索引的元素的列表,从而扩展到(但不包括)索引,直到此列表为止。

示例:

def test {
    expect (Cons("b", Cons("c", Nil()))) {
      slice(1, 3, Cons("a", Cons("b", Cons("c", Cons("d", Cons("e", Nil()))))))
    }
  }

另一个例子:

def test {
    expect (Cons("d", Cons("e", Nil()))) {
      slice(3, 7, Cons("a", Cons("b", Cons("c", Cons("d", Cons("e", Nil()))))))
    }
  }

这就是我所拥有的,但它不正确。有人可以帮我吗?

 abstract class StringList
    case class Nil() extends StringList
    case class Cons(h: String, t: StringList) extends StringList

    object Solution { 

      // define function slice
      def slice(from : Int, until : Int, xs : StringList) : StringList = (from, until, xs) match {
        case (_,_,Nil()) => Nil()
        case (n, m, _) if(n == m) => Nil()
        case (n, m, _) if(n > m) => Nil()
        case (n, m, _) if(n < 0) => Nil()
        case (n, m, xs) if(n == 0)=> Cons(head(xs), slice(n+1,m,tail(xs)))
        case (n, m, xs) => {
          //Cons(head(xs), slice(n+1,m,tail(xs)))
          if(n == from) { 
            print("\n") 
            print("n == m " + Cons(head(xs), slice(n+1,m,tail(xs)))) 
            print("\n")  
            Cons(head(xs), slice(n+1,m,tail(xs)))
          }
          else slice(n+1,m,tail(xs))
        }
      }

      def head(t : StringList) : String = t match {
        case Nil() => throw new NoSuchElementException 
        case Cons(h, t) => h
      }

      def tail(t : StringList) : StringList = t match {
        case Nil() => Nil()
        case Cons(h, t) => t
      }

     /* def drop(n : Int, t : StringList) : StringList = (n, t) match {
        case (0, t) => t
        case (_, Nil()) => Nil()
        case (n, t) => drop(n-1 , tail(t))
      }*/

    }//

1 个答案:

答案 0 :(得分:1)

这样可以添加一个方法来查找给定索引处的元素:

    trait StringList

  case class Nil() extends StringList

  case class Cons(h: String, t: StringList) extends StringList

  object Solution {

    def numberOfElements(str: StringList, count: Int = 0): Int = {
      if (str == Nil()) count else numberOfElements(tail(str), count + 1)
    }

    def elemAtIndex(n: Int, str: StringList, count: Int = 0): String = {
      if (str == Nil() || n == count) head(str) else elemAtIndex(n, tail(str), count + 1)
    }

    def head(str: StringList): String = str match {
      case Nil() => throw new NoSuchElementException
      case Cons(h, t) => h
    }

    def tail(str: StringList): StringList = str match {
      case Nil() => Nil()
      case Cons(h, t) => t
    }

    // define function slice
    def slice(from: Int, until: Int, xs: StringList): StringList = (from, until, xs) match {
      case (n, m: Int, _) if n == m || n > m || n < 0 => Nil()
      case (n, m: Int, xs: StringList) =>
        if (m > numberOfElements(xs)) {
          slice(n, numberOfElements(xs), xs)
        } else {
          Cons(elemAtIndex(n, xs), slice(n + 1, m, xs))
        }
    }
  }

scala> Solution.slice(1, 3, Cons("a", Cons("b", Cons("c", Cons("d", Cons("e", Nil()))))))
res0: StringList = Cons(b,Cons(c,Nil()))

scala> Solution.slice(3, 7, Cons("a", Cons("b", Cons("c", Cons("d", Cons("e", Nil()))))))
res0: StringList = Cons(d,Cons(e,Nil()))