《Two Dozen Short Lessons in Haskell》学习(三)

《Two Dozen Short Lessons in Haskell》(Copyright © 1995, 1996, 1997 by Rex Page,有人翻译为Haskell二十四学时教程,该书如果不用于赢利,可以任意发布,但需要保留他们的copyright)这本书是学习 Haskell的一套练习册,共有2本,一本是问题,一本是答案,分为24个章节。在这个站点有PDF文件。几年前刚开始学习Haskell的时候,感觉前几章还可以看下去,后面的内容越来越难以理解。现在对函数式编程有了一些了解后,再来看这些题,许多内容变得简单起来了。

初学Haskell之前一定要记住:

把你以前学习面向过程的常规的编程语言,如Pascal、C、Fortran等等统统忘在脑后,函数式编程完全是不一样的编程模型,用以前的术语和思维来理解函数式编程里的概念,只会让你困惑和迷茫,会严重地影响你的学习进度。

这个学习材料内容太多,想把整书全面翻译下来非常困难,只有通过练习题将一些知识点串起来,详细学习Haskell还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。

 

第三章 How to Run Haskell Programs

1 The following command

HASKELL COMMAND • :load script.hs

a loads script.hs into memory

b makes definitions in script.hs available for use in commands

c runs the commands in script.hs and reports results

d loads new definitions into script.hs, replacing the old ones

 

2 The following command

HASKELL COMMAND • :also script2.hs

a loads script.hs into memory

b adds definitions in script2.hs to those that are available for use in commands

c runs the commands in script2.hs and reports results

d tells the Haskell system that the definitions in script2.hs are correct

 

3 A Haskell system working in interactive mode

a interprets commands from the keyboard and responds accordingly

b acts like a general-purpose calculator

c is easier for novices to use than a batch-mode system

d all of the above

 

4 The following command

HASKELL COMMAND • :?

a initiates a query process to help find out what is running on the computer

b asks the Haskell system to display the results of its calculations

c displays a list of commands and explanations

d all of the above

 

=========================================================

=========================================================

答案:

1 b

Haskell的源程序文件名一般都用.hs当扩展名

以前Haskell的解释环境常用hugs,现在常用ghci或winghci,在这里你可以即时试验你写的函数的效果,查看它们的类型,还可以调试。

正如前面所说的,.hs里包含着一些定义definition,在ghci或winghci中执行:load script.hs命令,就是把这些命令装入到Haskell解释环境中,后面的交互指令就可以调用这些函数定义了。

:load命令本身并不执行这些函数。

 

2 b

:also是用来增加一些定义的,在ghci中不支持该命令。

 

3 d

ghci交互环境可以接受用户的输入,然后直接给出输出结果;当你输入一个数学函数时,直接给出结果,当然也可以当个计算器来使用;用这个交互环境可以在里面当场试验一些函数,比直接写代码要容易理解一些。

实际上使用ghc可以编译生成可执行的程序。

 

4 c

:? 就是:help,让ghci给出一些命令的使用帮助。

 

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 a command :<cmd>
  :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,等价于:l
  :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,等价于:r
  :run function [<arguments> ...] run the function with the given arguments
  :script <filename>          run the script <filename>
  :type <expr>                show the type of <expr> ,等价于:t
  :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     
  :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 languages             show the currently active language flags
  :show <setting>             show value of <setting>, which is one of
                                 [args, prog, prompt, editor, stop]



 

《Two Dozen Short Lessons in Haskell》学习(一)Hello World

《Two Dozen Short Lessons in Haskell》学习(二)Definitions

《Two Dozen Short Lessons in Haskell》学习(三)How to Run Haskell Programs

《Two Dozen Short Lessons in Haskell》学习(四)List Comprehensions

《Two Dozen Short Lessons in Haskell》学习(五)Function Composition and Currying

《Two Dozen Short Lessons in Haskell》学习(六)Patterns of Computation – Composition, Folding, and Mapping

《Two Dozen Short Lessons in Haskell》学习(七)- Types

《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism

《Two Dozen Short Lessons in Haskell》学习(九)- Types of Curried Forms and Higher Order Functions

《Two Dozen Short Lessons in Haskell》学习(十)- Private Definitions — the where-clause

《Two Dozen Short Lessons in Haskell》学习(十一)- Tuples

《Two Dozen Short Lessons in Haskell》学习(十二) 数值相关的类

《Two Dozen Short Lessons in Haskell》学习(十三)迭代及重复的常规模式

《Two Dozen Short Lessons in Haskell》学习(十四)截断序列和惰性求值

《Two Dozen Short Lessons in Haskell》学习(十五)- Encapsulation — modules

《Two Dozen Short Lessons in Haskell》学习(十六)- Definitions with Alternatives

《Two Dozen Short Lessons in Haskell》学习(十七) - 模块库

《Two Dozen Short Lessons in Haskell》学习(十八) - 交互式键盘输入和屏幕输出

《Two Dozen Short Lessons in Haskell》学习(十九) - 文件输入与输出

《Two Dozen Short Lessons in Haskell》学习(二十)- 分数

《Two Dozen Short Lessons in Haskell》学习(二十一)- 在形式参数中使用模式匹配

《Two Dozen Short Lessons in Haskell》学习(二十二)- 递归

第23章没有习题。

《Two Dozen Short Lessons in Haskell》(二十四)代数类型

posted @ 2012-11-08 20:59  申龙斌的程序人生  阅读(835)  评论(0编辑  收藏  举报