美文网首页
Haskell: 要点小记

Haskell: 要点小记

作者: 庞贝船长 | 来源:发表于2018-08-11 09:13 被阅读0次

0, 改变提示符 Prelude

:~$ ghci
----
Prelude> :set prompt "ghci> "
ghci> 

1, /= 表示 不等于(而不是自除!)(它看上去很像数学中的。)

ghci> 4 == 4
True
ghci> 4 /= 4
False

2, if-then-else: else 是强制要求的,有 if 必然有 else.

3, 在 ghci 输入 = 错误:

ghci> a=3

<interactive>:61:2: parse error on input ‘=’

改为:

ghci> let a = 3
ghci> a
3

详见: StackOverflow: Haskell error parse error on input `='

4, 在 Haskell 中, True, False 不定义为 1, 0 !! 非 0 值也不为 True!!

ghci> True && 1

<interactive>:6:9:
    No instance for (Num Bool) arising from the literal ‘1’
    In the second argument of ‘(&&)’, namely ‘1’
    In the expression: True && 1
    In an equation for ‘it’: it = True && 1

5, 否定: not 类C的语言中通常用!表示逻辑非的操作,而Haskell中用函数not

ghci> not True
False

6, 运算符优先级:Haskell给每个操作符一个数值型的优先级值,从1表示最低优先级,到9表示最高优先级。

ghci> :info (+)
class Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in ‘GHC.Num’
infixl 6 +
ghci> :info (*)
class Num a where
  ...
  (*) :: a -> a -> a
  ...
    -- Defined in ‘GHC.Num’
infixl 7 *

7, pi

ghci> pi
3.141592653589793

8, 幂数 ^: 用于整数幂; ** 用于浮点数幂

ghci> 2^3
8
ghci> 2**3.0
8.0

9, 左右全闭区间

ghci> [1..10]
[1,2,3,4,5,6,7,8,9,10]

10,"" 表示空字符串,它与 []同义

ghci> "" == []
True

11, 在 Haskell里,所有类型名字都以大写字母开头,而所有变量名字都以小写字母开头。

12, :set +t 打印出类型, :unset +t 取消打印出类型

ghci> :set +t
ghci> 'c'
'c'
it :: Char

ghci> :unset +t

13, [Char] 是 string 的别名

ghci> "foo"
"foo"
it :: [Char]

14, % 构建分数

-- :m 加载模块 Data.Ratio
ghci> :m +Data.Ratio  
ghci> 10 % 20
1 % 2
it :: Integral a => Ratio a

15, :type

ghci> 3 + 2
5
ghci> :type it
it :: Num a => a

相关文章

  • Haskell: 要点小记

    0, 改变提示符 Prelude 1, /= 表示 不等于(而不是自除!)(它看上去很像数学中的≠。) 2, if...

  • 函数式的宗教-00: 认识lisp(clojure)与haske

    总体大纲: lisp与haskell简单介绍 lisp与haskell应用领域 lisp与haskell技术分析 ...

  • monad以及monad的四条定理

    haskell的范畴是hask范畴(haskell的所有类型隶属于hask范畴),所以haskell的所有函子都是...

  • 01 数据类型

    swift中结构体在haskell中的描述 枚举类型在haskell中的描述 枚举携带类型在haskell中描述 ...

  • Haskell学习-函数式编程初探

    原文地址:Haskell学习-函数式编程初探  为什么要学习函数式编程?为什么要学习Haskell?  .net到...

  • Haskell

    [TOC] Haskell GHCI 通过Tab可以自动补全 通过 :browser 模块名称,浏览该模块下的函数...

  • haskell

    我在这里只是表达此刻内心想到的一些事情,或者记录自己关于最近学习生活工作的想法。 从我这一周对haskell的学习...

  • [Haskell] $

    函数“$”称为function application operator,定义如下: 与函数调用不同的是,函数调用...

  • [Haskell] .

    函数“.”称为function composition,定义如下: 我们看到,函数f接受函数g的返回值作为参数。函...

  • haskell

    stack --resolver lts-9 install ghc-mod Haskell-ghc-mod ::...

网友评论

      本文标题:Haskell: 要点小记

      本文链接:https://www.haomeiwen.com/subject/dbbrbftx.html