简体   繁体   English

haskell:了解类型和功能

[英]haskell: understanding types and functions

In an attempt to learn how haskell works, I created the following statements. 为了了解haskell的工作原理,我创建了以下语句。 I am trying to understand what their types are; 我试图了解它们的类型; can anybody let me know if I am the right track? 有人可以让我知道我是否正确吗?

statement                       type
['h', 'e', 'l', 'l', 'o']       ['h', 'e', 'l', 'l', 'o'] :: [char]

[(9,8),(7,6),(5,4)]             [(9,8),(7,6),(5,4)] :: [int a, int b] => [(a,b)] 

if that's correct, can someone help me understand the type / function for these statements: 如果是正确的话,有人可以帮助我了解这些语句的类型/功能:

  1. even x = x 'mod' 1 == 1

  2. chart xy = [y| x <- [1..x]]

Your type signatures are not quite correct. 您的类型签名不是很正确。 Pay attention to capitalisation, it's [Char] ; 注意大写,它是[Char] and it's Num and not int ; 它是Num而不是int and also wrong brackets: (Num a, Num b) => [(a, b)] . 以及错误的括号: (Num a, Num b) => [(a, b)] As for the rest... 至于其余的...

$ ghci
[...]
Prelude> let even x = x `mod` 1 == 1
Prelude> :t even
even :: Integral a => a -> Bool
Prelude> let chart x y = [y| x <- [1..x]]
Prelude> :t chart
chart :: (Enum t1, Num t1) => t1 -> t -> [t]

Also, note the backticks on the mod , not quotes. 另外,请注意mod上的反引号,而不是引号。

EDIT following the comments: 编辑以下评论:

It seems that you also want the clarification on the meaning of the functions even and chart . 似乎您还希望弄清函数evenchart的含义。

Even of x is the value of whether the remainder when you divide x by one is one x的偶数是将x除以1时余数是否为1的值

Unfortunately, the function is incorrectly written, since all integers are divisible by 1 , and the remainder is never 1 . 不幸的是,该函数的编写不正确,因为所有整数都可以被1整除,而余数永远不会为1 The correct definition for even is even的正确定义是

even x = x `mod` 2 == 0

Even of x is the value of whether the remainder when you divide x by two is zero x的偶数是将x除以2时余数是否为零的值

As for chart , 至于chart

Chart of a value x and a number y is a list consisting of values y for each element in a list of numbers from 1 to x 值x和数字y的图表是一个由值y组成的列表,该值由1到x的数字列表中的每个元素组成

so if you do chart 3 5 , you will have a list of threes for each element in [1, 2, 3, 4, 5], so five threes: [3, 3, 3, 3, 3] 因此,如果您执行chart 3 5 ,则在[1、2、3、4、5]中每个元素都会有一个三元组列表,所以有五个三元组: [3, 3, 3, 3, 3] 3、3、3、3、3 [3, 3, 3, 3, 3]

Types in Haskell are capitalized; Haskell中的类型大写; yes, it matters. 是的,这很重要。

['h', 'e', 'l', 'l', 'o'] :: [Char]

Correct, although [Char] and String are type synonyms, and String looks nicer. 正确,尽管[Char]和String是类型同义词,并且String看起来更好。 :P :P

[(9,8),(7,6),(5,4)] :: (Num a, Num b) => [(a,b)]

Multiple constraints are in round brackets. 多个约束放在括号中。 There's no "int" typeclass. 没有“ int”类型类。

even x = x `mod` 1 == 1
even :: Integral a => a -> Bool

Make sure you use backticks and not single quotes; 确保使用反引号而不是单引号; yes, it matters. 是的,这很重要。 In ghci use :t to check the type of mod and (==), and use :i to check the relationship between Num and Integral, and you should be able to put this together. 在ghci中,使用:t检查mod和(==)的类型,并使用:i检查Num和Integral之间的关系,您应该可以将它们放在一起。

chart x y = [y | x <- [1..x]]
chart :: (Num a, Enum a) => a -> b -> [b]

For this, you have to know the "long name" of [a..b] is enumFromTo, as well as how to desugar list comprehensions. 为此,您必须知道[a..b]的“长名”是enumFromTo,以及如何对列表推导去糖化。 List comprehensions aren't hard, but there's already good descriptions in RWH, LYAH, and the Haskell 2010 Report, so I won't leave one here. 列表理解并不难,但是在RWH,LYAH和《 Haskell 2010报告》中已经有很好的描述,因此我不会在这里留下一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM