试图在Purescript中使用Cons函数

dop*_*man 1 purescript

继承我的代码:

print 1: [2,3]
Run Code Online (Sandbox Code Playgroud)

当我跑它时,我得到了

  Cannot unify type
    Data.List.List
  with type
    Prim.Array
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?

Phi*_*man 6

[2, 3]有类型Array Int.(:)有类型a -> List a -> List aData.List.你需要转换为List.此外,你将解析为什么

(print 1) : [2, 3]
Run Code Online (Sandbox Code Playgroud)

我想你想要的

print (1 : toList [2, 3])
Run Code Online (Sandbox Code Playgroud)

要么

print $ 1 : toList [2, 3]
Run Code Online (Sandbox Code Playgroud)


小智 5

在psci中,看看(:)的类型

> :t (:)
forall a. a -> Data.List.List a -> Data.List.List a
Run Code Online (Sandbox Code Playgroud)

和[2,3]的类型

> :t [2, 3]
Prim.Array Prim.Int
Run Code Online (Sandbox Code Playgroud)

您可以看到(:)函数需要2个值:一个值和一个相同类型的List.在你的问题中,你给它一个Ints数组.您可以使用Data.List.toList函数来获取(:)期望的类型

> import Data.List
> show $ 1 : (toList [1, 2])
"Cons (1) (Cons (1) (Cons (2) (Nil)))"
Run Code Online (Sandbox Code Playgroud)