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

    第二十二章 Recursion

    递归是函数式编程中的主要编程思想,从而使得代码非常简练。

    1 Which of the following defines a function that delivers the same results as the intrinsic function reverse?

    a rev(x : xs) = xs ++ [x]

       rev [ ] = [ ]

    b rev(xs : x) = x : xs

       rev [ ] = [ ]

    c rev(x : xs) = rev xs ++ [x]

       rev [ ] = [ ]

    d none of the above

    2 Which of the following defines a function that would rearrange a sequence of numbers to put it in decreasing
    numeric order?

    a sortDecreasing = quickSortWith (>)

    b sortDecreasing = quickSortWith (>) [18.01528974, 1.89533e+25, 1.05522e-24, 27.0]

    c sortDecreasing = quickSortWith (>) numbers

    d all of the above

    3 The following function

    HASKELL DEFINITION • sorta(x : xs) = insert x (sorta xs)

    HASKELL DEFINITION • sorta [ ] = [ ]

    HASKELL DEFINITION • insert a (x : xs)

    HASKELL DEFINITION •     | a <= x = [a, x] ++ xs

    HASKELL DEFINITION •     | otherwise = [x] ++ (insert a xs)

    HASKELL DEFINITION • insert a [ ] = [a]

    a delivers the same results as quicksort

    b delivers the same results as quicksortWith (<)

    c both of the above

    d neither of the above

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

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

    1 c

    这道题很简单,如果函数定义里连rev都没有,根本就不是递归

    2 a

    选项b直接返回了一个确定性的列表,应该是[1.89533e+25,  27.0, 18.01528974, 1.05522e-24]

    选项c多了个numbers,这个函数就返回了一个列表,而不是返回一个函数

    3 c

    sorta就是插入排序

    quicksort默认是从小到大排序

  • 相关阅读:
    EF Power Tools
    ntsysv命令
    chpasswd 批量更新用户口令
    at定时执行任务命令详解
    shell
    为什么使用 shell 编程
    shell
    redis cluster 3.0
    CSS命名规则规范整理
    log4j:WARN No appenders could be found for logger
  • 原文地址:https://www.cnblogs.com/speeding/p/3143320.html
Copyright © 2011-2022 走看看