继承我的代码:
print 1: [2,3]
当我跑它时,我得到了
  Cannot unify type
    Data.List.List
  with type
    Prim.Array
这是怎么回事?
[2, 3]有类型Array Int.(:)有类型a -> List a -> List a在Data.List.你需要转换为List.此外,你将解析为什么
(print 1) : [2, 3]
我想你想要的
print (1 : toList [2, 3])
要么
print $ 1 : toList [2, 3]
小智 5
在psci中,看看(:)的类型
> :t (:)
forall a. a -> Data.List.List a -> Data.List.List a
和[2,3]的类型
> :t [2, 3]
Prim.Array Prim.Int
您可以看到(:)函数需要2个值:一个值和一个相同类型的List.在你的问题中,你给它一个Ints数组.您可以使用Data.List.toList函数来获取(:)期望的类型
> import Data.List
> show $ 1 : (toList [1, 2])
"Cons (1) (Cons (1) (Cons (2) (Nil)))"
| 归档时间: | 
 | 
| 查看次数: | 401 次 | 
| 最近记录: |