Jas*_*Jas 2 functional-programming scala
作者说:
def foo[A](fst: List[A], snd: List[A]): List[A]
我们实现该功能的方式较少.特别是,我们不能仅仅对列表中的某些元素进行硬编码,因为我们无法制作任意类型的值.
我不明白这一点,因为在[Char]版本中我们也没有能力制造任意类型的值,我们不得不使用类型,[Char]所以为什么实现这个的方法更少?
在通用版本,你知道,输出列表中只能包含包含的元素的一些安排fst和snd因为没有办法建立一些任意类型的新值A.相反,如果您知道输出类型,Char您可以例如
def foo(fst: List[Char], snd: List[Char]) = List('a', 'b', 'c')
Run Code Online (Sandbox Code Playgroud)
此外,您不能使用输入列表中包含的值来做出影响输出的决策,因为您不知道它们是什么.如果你知道输入类型,你可以这样做
def foo(fst: List[Char], snd: List[Char]) = fst match {
case Nil => snd
case 'a'::fs => snd
case _ => fst
}
Run Code Online (Sandbox Code Playgroud)