zoukankan      html  css  js  c++  java
  • 《Two Dozen Short Lessons in Haskell》学习(十五) Encapsulation — modules

    《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还是先看其它一些入门书籍吧,这本书配套着学学还是不错的。

    第十五章 封装--模块

    1 A Haskell module provides a way to

    a share variables and functions between scripts

    b hide some of the variables and functions that a script defines

    c package collections of variables and functions to be used in other scripts

    d all of the above

    2 The export list in a module designates variables and functions that

    a are defined in the module and redefined in other modules

    b are defined in the module and will be accessible to other scripts

    c are defined in other scripts and needed in the module

    d are defined in other scripts and redefined in the module

    3 An import specification in a script

    a makes all the definitions in a module available in the script

    b designates certain variables and functions in the script to be private

    c makes some public definitions from another module available for use in the script

    d specifies the importation parameters that apply in the script

    4 In a numeric representation scheme based on radix b,

    a numbers are denoted by sequences whose elements come from a set of b digits

    b numbers are written backwards

    c letters cannot be used to represent digits

    d numbers larger than b cannot be represented

    5 Horner’s formula

    a computes the reverse of a sequence of digits

    b takes too long to compute when n is bigger than 10

    c expresses a sum of multiples of powers of a certain base as a nest of products and sums

    d is too complicated to use in ordinary circumstances

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

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

    1 d

    Haskell也用模块来管理源代码,可以在不同的代码之间共享函数,隐藏一些变量和实现细节。

    module ModuleName (function1, function2)

    模块名必须是大写字母开头;

    括号里是输出列表(export list),表示这些函数可以共享给其它程序可用,类似java和C#等语言中的public关键字。

    不在输出列表中的函数都是private私有的,其它程序访问不到这些函数。

    通常的约定,一个.hs文件就是一个模块,文件名称就是模块名称。

    2 b

    模块里定义了一些函数,并提供一个输出列表,在这个输出列表上的函数,表示它们可以被其它模块访问。

    输出列表的定义方式就是把一些函数名称写在括号里面:

    module DecimalNumerals (integerFromDecimalNumeral, decimalNumeralFromInteger)

        where

        -- 后面写该模块中的函数定义

    3 c

    import语句使另外一个模块中的几个函数在当前这段程序中可见,与java语言类似,例如:

    import DecimalNumerals (integerFromDecimalNumeral, decimalNumeralFromInteger)

    引入DecimalNumerals模块中的两个函数, integerFromDecimalNumeral 和 decimalNumeralFromInteger这两个函数可以在当前程序里调用了。

    4 a

    该书中关于大数计算的例子让人感觉不太好理解,如果能换个大众容易理解的例子就好了。

    这里是一个关于b进制数的表达方法,这是的b是进制的基数,d0, d1, …, dn就是第0位、第1位、…、第n位上的数字,对于10进制数,当然这些d0, d1, …, dn数字的范围就是0-9,对于16进制,范围就是0-15。

    image

    5 c

    书中的horner公式用到了foldr函数

    horner b ds = foldr (multAdd b) 0 ds

    multAdd b d s = d + b*s

  • 相关阅读:
    从Unity3D编译器升级聊起Mono
    RxJava系列6(从微观角度解读RxJava源码)
    RxJava系列5(组合操作符)
    RxJava系列4(过滤操作符)
    RxJava系列3(转换操作符)
    RxJava系列2(基本概念及使用介绍)
    RxJava系列1(简介)
    给 Android 开发者的 RxJava 详解
    深入浅出RxJava就这一篇就够了
    android非法字符的判定、表情符号的判定
  • 原文地址:https://www.cnblogs.com/speeding/p/2921513.html
Copyright © 2011-2022 走看看