zoukankan      html  css  js  c++  java
  • Scala 函数式程序设计原理(2)--Higher Order Functions

    课程地址: https://www.coursera.org/learn/progfun1/lecture/xuM1M/lecture-2-1-higher-order-functions

    2.1 Higher-Order Functions

    Functional languages treat functions as first-class values.

    A function can be passed as a parameter and returned as a result.

    Functions that take other functions as parameters ot that return functions as results are called higher order functions.

    Anonymous Functions:

    (x1: T1, ..., xn: Tn) => E

    { def f(x1: T1, ..., xn: Tn) = E; f }

    2.2 Currying

    def sum(f: Int => Int)(a: Int, b: Int): Int ...

    type of sum: (Int => Int) => ((Int, Int) => Int)

      def mapReduce(f: Int => Int, combine: (Int, Int) => Int, zero: Int)(a: Int, b: Int): Int = {
        if (a > b) zero
        else combine(f(a), mapReduce(f, combine, zero)(a + 1, b))
      }
      def product(f: Int => Int)(a: Int, b: Int): Int = mapReduce(f, (x,y) => x*y, 1)(a, b)

    2.4 Scala Syntax Summary

    Extended Backus-Naur form (EBNF):

    | denotes an alternative, [...] an option (0 or 1), {...} a repetition (0 or more).

    A type can be:

    • A numeric type: Int, Double (and Byte, Short, Char, Long, Float),
    • The Boolean type with the values true and false,
    • The String type,
    • A function type, like Int => Int, (Int, Int) => Int.

    An expression can be:

    • An identifier such as x, isGoodEnough,
    • A literal, like 0, 1.0, "abc",
    • A function application, like sqrt(x),
    • An operator aplication, like -x, y+x,
    • A selection, like math.abs,
    • A conditional expression, like if (x < 0) -x else x,
    • A block, like { val x = math.abs(y); x * 2 },
    • An anonymous function, like x => x + 1.

    A definition can be:

    • A function definition, like def square(x: Int) = x * x,
    • A value definition, like val y = square(2)

    A parameter can be:

    • A call-by-value parameter, like (x: Int),
    • A call-by-name parameter, like (y: => Double).

    2.6 More Fun with Rationals

    Preconditions:

    require( y > 0, "denominator must be positive")

    Assertions:

    asser( x >= 0)

    difference:

    • require is used to enforce a precondition on the caller of a function.
    • assert is used as to check the code of the function itself.

    2.7 Evaluation and Operators

    class C(x1, ..., xm){ ... def f(y1, ..., ym) = b ... }

    new C(v1, ..., vm).f(w1, ..., wn) is rewritten to:

    [w1/y1, ..., wn/yn][v1/x1, ..., vm/xm][new C(v1, ..., vm)/this]b

    Example:

    new Rational(1, 2).numer

    -> [1/x, 2/y][][new Rational(1, 2)/this]x

    =1

    new Rational(1, 2).less(new Rational(2, 3))

    -> [1/x, 2/y][new Rational(2, 3)/that][new Rational(1, 2)/this]

    this.numer * that.denom < that.numer * this.denom

    = new Rational(1, 2).numer * new Rational(2, 3).denom < new Rational(2, 3).numer * new Rational(1, 2).denom

    = 1 * 3 < 2 * 2

    = true

    class Rational(x: Int, y: Int) {
      require(y != 0, "denominator must be nonzero")
    
      def this(x: Int) = this(x, 1)
      
      private def gcd(a: Int, b: Int): Int = if(b == 0) a else gcd(b, a % b)
      
      private val g = gcd(x, y)
      
      def numer = x / g
      def denom = y / g
      
      def < (that: Rational) = numer * that.denom < denom * that.numer
      def max(that: Rational) = if(this < that) that else this
      def + (that: Rational) = new Rational(numer*that.denom+denom*that.numer, denom*that.denom)
      def unary_- : Rational = new Rational(-numer, denom)
      def -(that: Rational) = this + (-that)
      
    }

    The precedence of an operator is determined by its first character.

    The following table lists the characters in increasing order of priority precedence:

    (all letters)

    |

    ^

    &

    < >

    = !

    :

    + -

    * / %

    (all other special characters)

  • 相关阅读:
    C++ 在名称空间中使用using声明和using编译指令
    jsp 之 解决mysql不是内部或外部命令问题
    一个男人关心的东西决定了他的层次 我在关心什么呢?
    初学安卓开发随笔之 启动活动的优化 用法
    初学安卓开发随笔之 Menu、toast 用法、活动的四种启动模式 以及 一个方便的Base活动类使用方法
    初学安卓开发随笔之 Intent 用法
    Python—数据类型之列表(List)
    Python—数据类型之字符串(String)
    Python—数据类型之数字(Number)
    斯里兰卡、马尔代夫旅游攻略
  • 原文地址:https://www.cnblogs.com/PaulingZhou/p/6857539.html
Copyright © 2011-2022 走看看