编译时出现Haskell类型错误 [英] Haskell type error on compilation

查看:194
本文介绍了编译时出现Haskell类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定为什么以下代码会导致以下错误。

代码:

  type Symbol = Char 

symbols :: [Symbol]
symbols = ['a'..'f']

类型代码= [Symbol]

成员::代码 - > Bool
会员xs =和[b | x < - xs,b < - map(elem x)symbols]

汇编错误:

 无法将类型'Char'与't0符号'匹配
预期类型: [t0符号]
实际类型:[符号]
•在'map'的第二个参数中,即'符号'
在表达式中:map(elem x)符号
在一个列表解析中:b < - map(elem x)symbols


解决方案


  1. 由于@FramkSchmitt提到了一个参数<$ c $您尝试将 elem x 映射到列表上 - 这将需要一个名为

这里是我猜你想要的东西。

  members :: Code  - > Bool 
成员xs =和[x`elem`符号| x <-xs]

可以写得更简洁一些(我相信像hlint这样的工具会甚至建议这种简化)。

  members':: Code  - > Bool 
members'=全部(`elem`符号)


I'm not sure why the following code is causing the following error.

Code:

type Symbol = Char

symbols :: [Symbol]
symbols = ['a'..'f']

type Code = [Symbol]

members :: Code -> Bool
members xs = and [ b | x <- xs, b <- map (elem x) symbols ]

Compilation Error:

Couldn't match type ‘Char’ with ‘t0 Symbol’
     Expected type: [t0 Symbol]
       Actual type: [Symbol]
   • In the second argument of ‘map’, namely ‘symbols’
     In the expression: map (elem x) symbols
     In a stmt of a list comprehension: b <- map (elem x) symbols

解决方案

The code you gave has a few errors.

  1. As @FramkSchmitt mentioned there is a parameter xs missing.
  2. you try to map elem x over a list - which would need a list of lists to be correct.

here is what I would guess what you intended.

members :: Code -> Bool
members xs = and [ x `elem` symbols  | x <- xs ]

which can be written a bit more concise (I believe tools like hlint would even suggest this simplification).

members' :: Code -> Bool
members' = all (`elem` symbols)

这篇关于编译时出现Haskell类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆