Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ls [T](hd : Option[T], tl : Ls[T]){
- def foldr[ACC](acc: ACC,fun:(ACC,Option[T])=>ACC):ACC = {
- hd match {
- case None => acc
- case Some(x) => tl.foldr(fun(acc,Some(x)),fun)
- }
- }
- def concat(x: T):Ls[T] = {
- new Ls[T](Some(x),this)
- }
- override def clone() = {
- this.foldr[Ls[T]](new Ls[T](None,null),(acc: Ls[T],x:Option[T]) => x match{
- case None => acc
- case Some(y) => acc.concat(y)
- })
- }
- def len():Int = {
- foldr[Int](0,(acc: Int,x:Option[T]) =>
- x match{
- case None => acc
- case Some(y) => acc+1
- }
- )
- }
- }
- object Main extends App{
- println("test")
- val ls = new Ls[Int](Some(1),new Ls[Int](Some(2),new Ls[Int](None,null)))
- println(ls.concat(1).clone().len())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement