''你好''''''世界''在Haskell中不起作用

问题描述 投票:1回答:1

只是想澄清这是我编程的第一天,我意识到这个问题是多么愚蠢:D

1)为什么不起作用?

ghci>''hello'' ++ ''world''

<interactive>:40:1: error
    * Syntax error on ''hello''
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    * In the Template Haskell quotation ''hello''

ghci>''hello'' ++ '' '' ++ ''world''

<interactive>:41:17: error: parse error on input '''

我应该在文本编辑器中添加一些东西才能使它工作吗?或者我只是做了一些菜鸟错误?

2)

ghci> ''Steve'' !! 2

<interactive>:42:2: error
    * Syntax error on ''Steve''
      Perhaps you intended to use TemplateHaskell or TemplateHaskellQuotes
    * In the Template Haskell quotation ''Steve''
ghci>[1,2,3,5,8] !! 2
3

当我使用数字执行这些命令时,它可以工作,但不能使用字符。我一定做错了什么,但我不确定是什么:/

提前致谢!

haskell
1个回答
10
投票

字符串被双引号(如"..")包围,而不是两个单引号(如''..'')。因此,您应该使用"而不是'

例如:

$ ghci
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
Prelude> "hello" ++ "world"
"helloworld"
Prelude> "hello" ++ " " ++ "world"
"hello world"

这在Haskell report 2010 section on 'Character and String Literals'中指定:

字符文字写在单引号之间,如'a'和双引号之间的字符串,如"Hello"

基于this article,可以使用Shift +'在Mac键盘上直接写双引号,这是它在“标准”querty键盘上的组织方式:

在任何典型的键盘上,只需按'直线单标记(')和Shift +'直线双标记(")。

基于qazxsw poi,对于英国qwerty键盘,qazxsw poi是Shift + 2

© www.soinside.com 2019 - 2024. All rights reserved.