Haskell中的单引号和双引号 [英] Single vs. double quotes in Haskell

查看:143
本文介绍了Haskell中的单引号和双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

  ghci> :t'a'
'a':: Char
ghci> :ta
a:: [Char]

为什么要治疗单引号和双引号的区别是不同的,并且是那么重要?解析方案

这与C / C ++ / C#/ Java和其他编程语言。单引号表示单字符,双引号表示字符数组(字符串)。

在Haskell中,'c'是一个字符( Char )和c是一个字符列表( [您可以将此与整数和整数列表进行比较:


pre> ghci的>让a = 1
ghci>让b = [1,2,3]
ghci> :t a
a :: Integer
ghci> :tb
b :: [Integer]

这很重要,因为必须有一个区别单个元素和元素列表之间。您可以对简单元素执行不同的操作,并对列表执行不同的操作。


Consider:

ghci> :t 'a'
'a' :: Char
ghci> :t "a"
"a" :: [Char]

Why is it treating single and double quotes differently, and is that important?

解决方案

This is just like C/C++/C#/Java and other programming languages. Single quotes means single character, double quotes means character array (string).

In Haskell, 'c' is a single character (Char), and "c" is a list of characters ([Char]).

You can compare this to integers and lists of integers:

ghci> let a = 1
ghci> let b = [1,2,3]
ghci> :t a
a :: Integer
ghci> :t b
b :: [Integer]

It is important because there has to be a distinction between single elements and a list of elements. You can perform different operations on simple elements, and different operations on lists.

这篇关于Haskell中的单引号和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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