2017-03-16 46 views
1

阅读this excellent intromatryoshka后,我试着变换IntList到一个通用的列表(我在下面的例子中保持了名IntList如何使用咖喱类型参数的套娃

sealed trait IntList[+H, +A] 
case class Cons[A](head: H, tail: A) extends IntList[H, A] 
case object Empty extends IntList[Nothing, Nothing] 

implicit def intListFunct[H]: Functor[IntList[H, ?]] = new Functor[IntList[H, ?]] { 
    def map[A, B](fa: IntList[H, A])(f: A => B): IntList[H, B] = fa match { 
    case Cons(head, tail) => Cons(head, f(tail)) 
    case Empty => Empty 
    } 
} 

def lst[T](implicit T: Corecursive.Aux[T, IntList[Int, ?]]): T = 
    Cons(
    1, 
    Cons(
     2, 
     Cons(
     3, 
     Empty.embed 
     ).embed 
    ).embed 
    ).embed 

def sumList[T](l: T)(implicit T: Recursive.Aux[T, IntList[Int, ?]]): Int = l.cata[Int] { 
    case Cons(head, tail) => head + tail 
    case Empty => 0 
} 

val listRes = sumList(lst[Fix[IntList[Int, ?]]]) 

,但它不编译我得到这个错误:

[error] /Users/caente1/workshop/graphs/src/main/scala/graph.scala:40: could not find implicit value for parameter T: matryoshka.Corecursive.Aux[matryoshka.data.Fix[[β$4$]common.lists.package.IntList[Int,β$4$]],[β$2$]common.lists.package.IntList[Int,β$2$]] 
[error] val listRes = sumList(lst[Fix[IntList[Int, ?]]]) 

所以我明明做错事,但我不能左右它让我的头呢,这将是更好的方式来做到这一点?

回答

2

在该指南的“示例重访”一节中,据说适用SI2712 fix - 您是否已经这么做?刚刚在命令行尝试过你的例子,它的工作。

+0

你是绝对正确的,修复它,谢谢! – caente