Advertisement
Guest User

fold

a guest
Jun 8th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.36 KB | None | 0 0
  1. class Ls [T](head: T,tail: Ls[T]){
  2.     val tl = tail
  3.     val hd = head
  4.     def foldr[ACC](acc: ACC,fun:(ACC,T)=>ACC):ACC = {
  5.       if (tl==null){
  6.         fun(acc,hd)
  7.       }else{
  8.          tl.foldr(fun(acc,hd),fun)
  9.       }
  10.     }
  11. }
  12. object Main extends App{
  13.     println("test")
  14.     val ls = new Ls[Int](1,new Ls[Int](2,new Ls[Int](3,null)))
  15.     println(ls.foldr[Int](0,(acc:Int,x:Int) => acc+x+1))
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement