GHCi Prelude学习

参考:http://www.cse.unsw.edu.au/~en1000/haskell/inbuilt.html

http://www.cse.unsw.edu.au/~en1000/haskell/hof.html

在GHCi中,可以使用:type来查看对象的类型,与http://www.cnblogs.com/long123king/p/3837686.html中说到的一样,

Haskell中,函数也是一种特殊的对象,对象就有类型,函数作为一种对象,可以作为参数传递,也可以赋值,创建和销毁。

Prelude> :type (+)
(+) :: Num a => a -> a -> a

  

这个类似要怎么解释呢,"::"的前面是函数的名称,后面是函数对象的类型,或者说原型。

"=>"前面的Num a是表明参数的类型,

Prelude> :type 1
1 :: Num a => a

后面是函数的输入与输出类型声明。

之所以会有多个->,那是因为(+)函数对象中其实包含一个更加简单的函数,比如(+) 2,这个函数的意思是“在使用(+)函数对象时,将第一个参数固定为2,这与boost中的bind类似”,而这个函数对象的类型是Num a => a->a,

再把另外一个参数传递给这个简单的函数,得到的结果也是a类型,因此(+)是一个复合函数。

凡是需要多个参数的函数对象,都可以分解成一步一步的简单函数组成的复合函数。

Prelude> (+) 2

<interactive>:35:1:
    No instance for (Num a0) arising from a use of `+‘
    The type variable `a0‘ is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Num Double -- Defined in `GHC.Float‘
      instance Num Float -- Defined in `GHC.Float‘
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real‘
      ...plus three others
    In the expression: (+) 2
    In an equation for `it‘: it = (+) 2

<interactive>:35:1:
    No instance for (Show (a0 -> a0)) arising from a use of `print‘
    Possible fix: add an instance declaration for (Show (a0 -> a0))
    In a stmt of an interactive GHCi command: print it
Prelude> :type (+) 2
(+) 2 :: Num a => a -> a

  

特殊符号需要显式地用括号来表明这是个函数对象,

对于普通的函数对象,也可以使用括号来表明其函数对象的身份。

Prelude> :type +

<interactive>:1:1: parse error on input `+‘
Prelude> :type (+)
(+) :: Num a => a -> a -> a
Prelude> :type names
names :: [Char]
Prelude> :type head
head :: [a] -> a
Prelude> :type (head)
(head) :: [a] -> a

再来看一个更加复杂的函数对象

Prelude> :type map
map :: (a -> b) -> [a] -> [b]

  

这个函数对象包含了两个简单函数对象, (a -> b)是一个函数,可以将类型a的对象转换成类型b的对象;

(a -> b) -> [a],是另外一个函数,它的意思是“在执行整体函数对象时,将第一个参数固定为[a]”。

这种将复合函数对象(包含了多个参数的函数对象)分解成几个简单函数对象的思想,是为了支持“函数作为一种对象”的这种设计理念,

这样就可以将上面分解出的简单的函数作为参数,传递给复合函数对象。

比如

Prelude> let nums = [1..100]
Prelude> map ((*) 2) nums
[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196,198,200]

  

  

虽然,像(+) 2这样的函数对象在多数时候,看起来并不是一个常见的用法。

怎样在Prelude中得到当前运行环境的信息

Prelude> :help
 Commands available from the prompt:

   <statement>                 evaluate/run <statement>
   :                           repeat last command
   :{\n ..lines.. \n:}\n       multiline command
   :add [*]<module> ...        add module(s) to the current target set
   :browse[!] [[*]<mod>]       display the names defined by module <mod>
                               (!: more details; *: all top-level names)
   :cd <dir>                   change directory to <dir>
   :cmd <expr>                 run the commands returned by <expr>::IO String
   :ctags[!] [<file>]          create tags file for Vi (default: "tags")
                               (!: use regex instead of line number)
   :def <cmd> <expr>           define command :<cmd> (later defined command has
                               precedence, ::<cmd> is always a builtin command)
   :edit <file>                edit file
   :edit                       edit last module
   :etags [<file>]             create tags file for Emacs (default: "TAGS")
   :help, :?                   display this list of commands
   :info [<name> ...]          display information about the given names
   :issafe [<mod>]             display safe haskell information of module <mod>
   :kind <type>                show the kind of <type>
   :load [*]<module> ...       load module(s) and their dependents
   :main [<arguments> ...]     run the main function with the given arguments
   :module [+/-] [*]<mod> ...  set the context for expression evaluation
   :quit                       exit GHCi
   :reload                     reload the current module set
   :run function [<arguments> ...] run the function with the given arguments
   :script <filename>          run the script <filename>
   :type <expr>                show the type of <expr>
   :undef <cmd>                undefine user-defined command :<cmd>
   :!<command>                 run the shell command <command>

 -- Commands for debugging:

   :abandon                    at a breakpoint, abandon current computation
   :back                       go back in the history (after :trace)
   :break [<mod>] <l> [<col>]  set a breakpoint at the specified location
   :break <name>               set a breakpoint on the specified function
   :continue                   resume after a breakpoint
   :delete <number>            delete the specified breakpoint
   :delete *                   delete all breakpoints
   :force <expr>               print <expr>, forcing unevaluated parts
   :forward                    go forward in the history (after :back)
   :history [<n>]              after :trace, show the execution history
   :list                       show the source code around current breakpoint
   :list identifier            show the source code for <identifier>
   :list [<module>] <line>     show the source code around line number <line>
   :print [<name> ...]         prints a value without forcing its computation
   :sprint [<name> ...]        simplifed version of :print
   :step                       single-step after stopping at a breakpoint
   :step <expr>                single-step into <expr>
   :steplocal                  single-step within the current top-level binding
   :stepmodule                 single-step restricted to the current module
   :trace                      trace after stopping at a breakpoint
   :trace <expr>               evaluate <expr> with tracing on (see :history)

 -- Commands for changing settings:

   :set <option> ...           set options
   :seti <option> ...          set options for interactive evaluation only
   :set args <arg> ...         set the arguments returned by System.getArgs
   :set prog <progname>        set the value returned by System.getProgName
   :set prompt <prompt>        set the prompt used in GHCi
   :set editor <cmd>           set the command used for :edit
   :set stop [<n>] <cmd>       set the command to run when a breakpoint is hit
   :unset <option> ...         unset options

  Options for ‘:set‘ and ‘:unset‘:

    +m            allow multiline commands
    +r            revert top-level expressions after each evaluation
    +s            print timing/memory stats after each evaluation
    +t            print type after evaluation
    -<flags>      most GHC command line flags can also be set here
                         (eg. -v2, -fglasgow-exts, etc.)
                    for GHCi-specific flags, see User‘s Guide,
                    Flag reference, Interactive-mode options

 -- Commands for displaying information:

   :show bindings              show the current bindings made at the prompt
   :show breaks                show the active breakpoints
   :show context               show the breakpoint context
   :show imports               show the current imports
   :show modules               show the currently loaded modules
   :show packages              show the currently active package flags
   :show language              show the currently active language flags
   :show <setting>             show value of <setting>, which is one of
                                  [args, prog, prompt, editor, stop]
   :showi language             show language flags for interactive evaluation

  

Prelude> :show modules
Prelude> :show context

Prelude> :show bindings
names :: [Char] = "Daniel King"
nums :: [Integer] = 1 : 2 : 3 : 4 : 5 : ....
it :: [Integer] = 2 : 4 : 6 : 8 : 10 : ....
Prelude> :show imports
import Prelude -- implicit
Prelude> :show packages
active package flags: none
Prelude> :show languages
base language is: Haskell2010
with the following modifiers:
  -XNoDatatypeContexts
  -XNondecreasingIndentation

  

Prelude> foldl ((+)) 0 [1..100]
5050

  

Prelude> :type map
map :: (a -> b) -> [a] -> [b]
Prelude> :type filter
filter :: (a -> Bool) -> [a] -> [a]
Prelude> :type foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
Prelude> :type foldl
foldl :: (a -> b -> a) -> a -> [b] -> a

  

  

Prelude> filter ((>) 50) nums
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49]

  

Prelude> :type foldr
foldr :: (a -> b -> b) -> b -> [a] -> b
Prelude> foldr (:) "King" [‘D‘,‘a‘,‘n‘,‘i‘,‘e‘,‘l‘, ‘ ‘]
"Daniel King"

  

但是用foldl就不行。

GHCi Prelude学习

时间: 2024-10-08 10:17:10

GHCi Prelude学习的相关文章

Python Twisted 学习系列21(转载stulife最棒的Twisted入门教程)

第二十一部分 惰性不是迟缓: Twisted和Haskell 简介 在上一个部分我们对比了Twisted与 Erlang,并将注意力集中在它们共有的一些思想上.结果表明使用Erlang也是非常简便的,因为异步I/O和反应式编程是Erlang运行时和进程模型的关键元素. 今天我们想走得更远一点,去看一看 Haskell —— 另一种功能性语言,然而与Erlang有很大不同(当然与Python也不同).这里面没有太多的平行概念,但我们仍然会发现藏在下面的异步I/O概念. F —— 功能性 虽然Erl

Haskell ghci中调用pandoc的API进行markdown转换

所用环境:Windows Server 2008 + ghc 7.6.3(Haskell Platform 2013.2.0.0自带的) + pandoc 1.12.4 操作步骤: 1. 安装Haskell Platform,下载地址:http://www.haskell.org/platform/. 2. 安装pandoc,安装命令:cabal install pandoc 3. 在命令行中运行ghci 4. 引用pandoc的相应模块,在Prelude命令提示符中运行: :module Te

JTSL/EL Expression学习

最早的一个学习笔记,时间过去了久了,供java web初学者参考. JTSL/EL Expression学习安排 学习目标:掌握几个常见标签的使用,通晓工作原理,详细到代码层面,遇到问题时能查得出异常,能排除异常. 学习时间:2天 a) 掌握逻辑判断标签<c:if>,<c:choose>,<c:when>,<c:otherwise> b) 掌握EL表达式页面隐式对象:requestScope,sessionScope,pageScope,pageContex

Real World Haskell学习篇-第1章: 入门

1. 初识解释器ghci 1.1  查看帮助: :? 1.2  修改提示符: :set prompt ghci>>> 1.3  加自己指定模块: :module + Data.Ratio 2. 基本交互 2.1 基本算术运算 中缀表达式: 1 ghci>>> 3 ^ 3 2 27 3 ghci>>> 2 + 4 4 6 5 ghci>>> 5 / 3 6 1.6666666666666667 前缀表达式: 1 ghci>>

Haskell学习笔记二:自定义类型

内容提要: 代数数据类型 - Algebraic Data Types: 自定义数据类型 - data关键字:值构造器:类型变量与类型构造器: 记录(Record)语法 - 简化自定义数据类型的一种语法糖: 一个完整的例子 - PurchaseOrder定义和简单计算.单元测试: 代数数据类型(Algebraic Data Types) 为什么Haskell的数据类型会有代数数据类型这个名字?回想我们初中时代,初次学习代数的情况,印象最深刻就是x,y,z代替了具体的数字,引入方程式的概念,对 解

Haskell的基本语法和GHCi

1.注释 a) --这是一行注释 b) {- 这是一段注释 -} 2.表达式 a) 3 :: Float 标示是一个浮点数3,::起到说明类型的作用 b) sort [3,8,1,4] 是一次函数调用.Haskell中的函数调用不需要加括号,多个参数中间也不用加逗号. c) Case Foo of True -> 1 False ->2 最后一个表达式的值在Foo是True时等于1,否则等于2. 值得注意的是:if...then...else和case...of...这些语句虽然在其他语言中作

Haskell学习笔记一:类型和类型类相关内容

内容提要: 静态类型系统: 编译时确定类型错误: 类型推导机制: 基础类型:Int,Integer,Float,Double,Bool,Char: 类型变量: 基础类型类:Eq,Ord,Show,Read,Enum,Bounded,Num,Integral,Floating: Haskell是一门函数式编程语言,被称为最为纯粹的函数式编程语言.Haskell的类型系统非常强大,其中包含了很多有趣.抽象.某种程度上充满学术气息的特质. Haskell属于静态类型语言,这意味着: 每个值或者表达式,

Haskell语言学习笔记(64)Lens(4)

Prisms data NewTask = SimpleTask String | HarderTask String Int | CompoundTask String [NewTask] deriving (Show) makePrisms ''NewTask *Main> a ^? _SimpleTask Just "Clean" *Main> b ^? _HarderTask Just ("Clean Kitchen",15) *Main>

actix rust actor 框架学习 二 ping actor demo 代码

以下是官方文档的学习,了解基本的actix actor 编程模型 项目初始化 cargo 创建 cargo new actor-ping --bin 效果 ├── Cargo.toml └── src    └── main.rs 添加依赖 cargo.toml 配置 [package] name = "actor-ping" version = "0.1.0" authors = ["rongfengliang <[email protected]