haskell - 对由一个列表组成的自身类型的列表操作
问题描述
我得到了类型“ Signal
”,它只是一个双打列表,我想使用普通的列表操作,比如在 native [Double]
. 这应该是可能的,但它不会编译。您将如何在语法上正确地向编译器描述这一点。
这是测试代码:
data Signal = Signal [Double] deriving(Show, Eq)
test :: Signal
test = Signal [1.0,2.0,3.0]
take2 :: Signal -> Signal
take2 s = take 2 s
预期的:take2 test = [1.0,2.0]
实际的:
"Couldn't match expected type ‘Signal’ with actual type ‘[a0]’
In the expression: take 2 s
In an equation for ‘take2’: take2 s = take 2 s"
解决方案
注意函数定义如下:
take2 :: Signal -> Signal
take2 s = take 2 s
s
不是列表,它是 aSignal
并且take 2 s
是列表,而不是 a Signal
,您需要的是从列表中获取列表并从列表中Signal
构造 aSignal
为:
take2 (Signal s) = Signal $ take 2 s
推荐阅读
- docker - 如何在 Docker Desktop 中为选定容器打开 CLI 时将默认 /bin/sh 更改为 /bin/bash?
- django - Heroku 登录失败:错误:尝试部署 django 应用程序时无法获取本地颁发者证书
- django - 如何在模型名称订单的单个字段中由用户获取购物车的所有项目
- asp.net - 从 Model.Count() 函数中删除分号
- pandas - 如何为对象类型空白元素分配一些字符串值
- reactjs - ReactToastify 位置在 React 中不起作用
- jmeter - JMeter 脚本记录 - 同一个请求,在同一个事务中多次
- java - 如何在 MongoDB Java 驱动程序 3.0 中获取批量写入指标
- python - 如何使用 geopandas 找到坐标的最近值
- html - 带有活动链接的 mouseenter 上的引导下拉菜单