参考: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中,函数也是一种特殊的对象,对象就有类型,函数作为一种对象,可以作为参数传递,也可以赋值,创建和销毁。

  1. Prelude> :type (+)
  2. (+) :: Num a => a -> a -> a

  

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

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

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

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

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

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

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

  1. Prelude> (+) 2
  2.  
  3. <interactive>:35:1:
  4. No instance for (Num a0) arising from a use of `+'
  5. The type variable `a0' is ambiguous
  6. Possible fix: add a type signature that fixes these type variable(s)
  7. Note: there are several potential instances:
  8. instance Num Double -- Defined in `GHC.Float'
  9. instance Num Float -- Defined in `GHC.Float'
  10. instance Integral a => Num (GHC.Real.Ratio a)
  11. -- Defined in `GHC.Real'
  12. ...plus three others
  13. In the expression: (+) 2
  14. In an equation for `it': it = (+) 2
  15.  
  16. <interactive>:35:1:
  17. No instance for (Show (a0 -> a0)) arising from a use of `print'
  18. Possible fix: add an instance declaration for (Show (a0 -> a0))
  19. In a stmt of an interactive GHCi command: print it
  20. Prelude> :type (+) 2
  21. (+) 2 :: Num a => a -> a

  

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

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

  1. Prelude> :type +
  2.  
  3. <interactive>:1:1: parse error on input `+'
  4. Prelude> :type (+)
  5. (+) :: Num a => a -> a -> a
  6. Prelude> :type names
  7. names :: [Char]
  8. Prelude> :type head
  9. head :: [a] -> a
  10. Prelude> :type (head)
  11. (head) :: [a] -> a

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

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

  

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

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

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

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

比如

  1. Prelude> let nums = [1..100]
  2. Prelude> map ((*) 2) nums
  3. [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中得到当前运行环境的信息

  1. Prelude> :help
  2. Commands available from the prompt:
  3.  
  4. <statement> evaluate/run <statement>
  5. : repeat last command
  6. :{\n ..lines.. \n:}\n multiline command
  7. :add [*]<module> ... add module(s) to the current target set
  8. :browse[!] [[*]<mod>] display the names defined by module <mod>
  9. (!: more details; *: all top-level names)
  10. :cd <dir> change directory to <dir>
  11. :cmd <expr> run the commands returned by <expr>::IO String
  12. :ctags[!] [<file>] create tags file for Vi (default: "tags")
  13. (!: use regex instead of line number)
  14. :def <cmd> <expr> define command :<cmd> (later defined command has
  15. precedence, ::<cmd> is always a builtin command)
  16. :edit <file> edit file
  17. :edit edit last module
  18. :etags [<file>] create tags file for Emacs (default: "TAGS")
  19. :help, :? display this list of commands
  20. :info [<name> ...] display information about the given names
  21. :issafe [<mod>] display safe haskell information of module <mod>
  22. :kind <type> show the kind of <type>
  23. :load [*]<module> ... load module(s) and their dependents
  24. :main [<arguments> ...] run the main function with the given arguments
  25. :module [+/-] [*]<mod> ... set the context for expression evaluation
  26. :quit exit GHCi
  27. :reload reload the current module set
  28. :run function [<arguments> ...] run the function with the given arguments
  29. :script <filename> run the script <filename>
  30. :type <expr> show the type of <expr>
  31. :undef <cmd> undefine user-defined command :<cmd>
  32. :!<command> run the shell command <command>
  33.  
  34. -- Commands for debugging:
  35.  
  36. :abandon at a breakpoint, abandon current computation
  37. :back go back in the history (after :trace)
  38. :break [<mod>] <l> [<col>] set a breakpoint at the specified location
  39. :break <name> set a breakpoint on the specified function
  40. :continue resume after a breakpoint
  41. :delete <number> delete the specified breakpoint
  42. :delete * delete all breakpoints
  43. :force <expr> print <expr>, forcing unevaluated parts
  44. :forward go forward in the history (after :back)
  45. :history [<n>] after :trace, show the execution history
  46. :list show the source code around current breakpoint
  47. :list identifier show the source code for <identifier>
  48. :list [<module>] <line> show the source code around line number <line>
  49. :print [<name> ...] prints a value without forcing its computation
  50. :sprint [<name> ...] simplifed version of :print
  51. :step single-step after stopping at a breakpoint
  52. :step <expr> single-step into <expr>
  53. :steplocal single-step within the current top-level binding
  54. :stepmodule single-step restricted to the current module
  55. :trace trace after stopping at a breakpoint
  56. :trace <expr> evaluate <expr> with tracing on (see :history)
  57.  
  58. -- Commands for changing settings:
  59.  
  60. :set <option> ... set options
  61. :seti <option> ... set options for interactive evaluation only
  62. :set args <arg> ... set the arguments returned by System.getArgs
  63. :set prog <progname> set the value returned by System.getProgName
  64. :set prompt <prompt> set the prompt used in GHCi
  65. :set editor <cmd> set the command used for :edit
  66. :set stop [<n>] <cmd> set the command to run when a breakpoint is hit
  67. :unset <option> ... unset options
  68.  
  69. Options for ':set' and ':unset':
  70.  
  71. +m allow multiline commands
  72. +r revert top-level expressions after each evaluation
  73. +s print timing/memory stats after each evaluation
  74. +t print type after evaluation
  75. -<flags> most GHC command line flags can also be set here
  76. (eg. -v2, -fglasgow-exts, etc.)
  77. for GHCi-specific flags, see User's Guide,
  78. Flag reference, Interactive-mode options
  79.  
  80. -- Commands for displaying information:
  81.  
  82. :show bindings show the current bindings made at the prompt
  83. :show breaks show the active breakpoints
  84. :show context show the breakpoint context
  85. :show imports show the current imports
  86. :show modules show the currently loaded modules
  87. :show packages show the currently active package flags
  88. :show language show the currently active language flags
  89. :show <setting> show value of <setting>, which is one of
  90. [args, prog, prompt, editor, stop]
  91. :showi language show language flags for interactive evaluation

  

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

  

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

  

  1. Prelude> :type map
  2. map :: (a -> b) -> [a] -> [b]
  3. Prelude> :type filter
  4. filter :: (a -> Bool) -> [a] -> [a]
  5. Prelude> :type foldr
  6. foldr :: (a -> b -> b) -> b -> [a] -> b
  7. Prelude> :type foldl
  8. foldl :: (a -> b -> a) -> a -> [b] -> a

  

  

  1. Prelude> filter ((>) 50) nums
  2. [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]

  

  1. Prelude> :type foldr
  2. foldr :: (a -> b -> b) -> b -> [a] -> b
  3. Prelude> foldr (:) "King" ['D','a','n','i','e','l', ' ']
  4. "Daniel King"

  

但是用foldl就不行。

使用子模块功能

  1. Prelude> map Data.Char.isDigit ((++) ['0'..'9'] ['a'..'z'])
  2. [True,True,True,True,True,True,True,True,True,True,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False]
  3. Prelude> map Data.Char.isDigit (concat [['0'..'9'],['a'..'z']])
  4. [True,True,True,True,True,True,True,True,True,True,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False]

  

另外,(++) 与concat的功能不相同,它们的原型也不相同,因此在使用一个函数之前,一定要明确这个函数的原型。

GHCi Prelude学习的更多相关文章

  1. 「Haskell 学习」二 类型和函数(上)

    随着学习的深入,笔记会补充和修订.当然,这个补充修订也许会鸽,但我一定会坚持写完. 这个笔记假定你至少学过C/C++及Python,或与这两种语言类型相同的语言. 类型系统概述 “Haskell’s ...

  2. Haskell语言学习笔记(47)Arrow(2)

    Function, Monad, Arrow f :: Int -> (Int, Int) f = \x -> let y = 2 * x z1 = y + 3 z2 = y - 5 in ...

  3. JTSL/EL Expression学习

    最早的一个学习笔记,时间过去了久了,供java web初学者参考. JTSL/EL Expression学习安排 学习目标:掌握几个常见标签的使用,通晓工作原理,详细到代码层面,遇到问题时能查得出异常 ...

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

    所用环境:Windows Server 2008 + ghc 7.6.3(Haskell Platform 2013.2.0.0自带的) + pandoc 1.12.4 操作步骤: 1. 安装Hask ...

  5. 「Haskell 学习」一 环境与大致了解

    感谢<Real World Haskell>在网上的免费发布,可以白嫖学Haskell这个久闻大名的函数式编程语言了. 本文运行于openSUSE Tumbleweed下,运行相关命令时留 ...

  6. Emacs 学习之旅

    **Emacs 的使用过程,就像是程序员的生涯一样--路漫漫其修远兮,吾将上下而求索.** ## 万物始于 Emacs 最早知道 _Emacs_ 是从编辑器的圣战开始的,即编辑器之神--Vi,和神的编 ...

  7. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  8. Angular2学习笔记(1)

    Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...

  9. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

随机推荐

  1. MFC---导出 Excel 方法

    本方法通过Excel驱动写入 请添加头文件 #include"afxdb.h" 第一步创建Excel文件 安装驱动 CString FileName = L"first. ...

  2. [BZOJ3932][CQOI2015]任务查询系统(差分+主席树)

    题面 分析 对于一个区间修改(s,e,v),我们可以将它差分,这样就变成了单点修改s和e+1(s插入,t+1删除) 我们用主席树维护差分数组的前缀和,第i棵主席树维护区间[1,i]之间的所有差分值 那 ...

  3. #python# error:urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

    设置代理后访问网页报错,百度有人说地址拼写不对,确认拼写后依然报错 因为使用的是xici免费代理,想到可能代理不可用造成getaddrinfo failed, 更换其他代理,error消失

  4. vue-cli3脚手架的安装

    如果之前有安装过其他的版本的话,要先卸载 卸载:npm uninstall vue-cli-g  或  yarn global remove vue-cli 安装:npm i @vue/cli -g ...

  5. Django 项目环境搭建

    环境 mkvirtualenv -p python3.6 mytest # 创建虚拟环境 workon mytest # 进入虚拟环境 # 按照基础环境依赖 pip install django==1 ...

  6. try、catch、finally--try块里有return,finally还执行吗?

    finally块的作用是,保证无论出现什么情况,finally块里的代码一定会被执行. 由于程序执行return就意味着结束对当前函数的调用并跳出这个函数体,所以任何语句要执行都只能在return之前 ...

  7. 【LeetCode】随机化算法 random(共6题)

    [384]Shuffle an Array(2019年3月12日) Shuffle a set of numbers without duplicates. 实现一个类,里面有两个 api,struc ...

  8. spring整合Quartz2持久化任务调度

    转摘 https://blog.csdn.net/qwe6112071/article/details/50999386 因为通过Bean配置生成的JobDetail和CronTrigger或Simp ...

  9. MySQL MHA + Ifconfig管理vip

    前期的安装步骤,还是参照:http://www.cnblogs.com/yiyuf/p/4104354.html进行,只需要把appxxx.cnf中定义的相关.sh脚本(如:master_ip_fai ...

  10. grep正则表达式(一)

    新建一批 txt 文件: [me@linuxbox ~]$ ls /bin > dirlist-bin.txt [me@linuxbox ~]$ ls /usr/bin > dirlist ...