楼主: ReneeBK
766 9

[Lecture Notes]Functional Programming,University of Mississippi [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49412 个
通用积分
51.9904
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
CSci 555: Functional Programming
Spring Semester 2016
Lecture Notes





二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming University Functional Universit function University

沙发
ReneeBK 发表于 2016-8-20 01:00:03 |只看作者 |坛友微信交流群
  1. Feb 10 08:27 2016 sqrt.scala Page 1


  2. /* Square Root by Newton's Method -- Version 1
  3.    Functional Programming Example
  4.    H. Conrad Cunningham, Professor, Computer and Information Science
  5.    University of Mississippi

  6.    Developed for CSci 555, Functional Programming, Spring 2016

  7. 1234567890123456789012345678901234567890123456789012345678901234567890

  8. 2016-02-10: Developed from 2015 Elixir version

  9. Function sqrt computes the square root of its argument x using
  10. Newton's method.  

  11. I adapted this square root module from an Elixir version, which was,
  12. in turn, adapted from a Lua version, which was, in turn, adapted from
  13. the Scheme code in section 1.1.7 of Abelson and Sussman's Structure
  14. and Interpretation of Computer Programs (SICP) textbook. I changed the
  15. function names to better match the Scala naming convention.

  16. I have only tested this code minimally.

  17. Scala and functional programming highlights:
  18. - Uses object to define a module
  19. - Uses type inference as much as possible (But I suggest it is
  20.   better to give return types for public functions.)
  21. - Requires return type for sqrtIter because it is recursive
  22. - Makes all functions visible.  Should hide all but sqrt.
  23. - Uses tail recursion
  24. - Calls Math.abs.

  25. */

  26. object Sqrt {

  27.   def sqrt(x: Double) = sqrtIter(1.0, x)

  28.   // Tail recursive auxiliary function
  29.   def sqrtIter(guess: Double, x: Double) : Double =
  30.     if (isGoodEnough(guess,x))
  31.       guess
  32.     else
  33.       sqrtIter(improve(guess,x),x)

  34.   def isGoodEnough(guess: Double, x: Double) =
  35.     Math.abs(square(guess)- x)< 0.001

  36.   def square(x: Double) = x * x

  37.   def improve(guess: Double, x: Double) = average(guess,x/guess)

  38.   def average(x: Double, y: Double) = (x + y) / 2








  39. Feb 10 08:27 2016 sqrt.scala Page 2


  40.   // Should implement extensive tesing externally
  41.   def main(args: Array[String]) {
  42.     println("square root of 9 is " + sqrt(9.0))
  43.   }

  44. }
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-8-20 01:02:57 |只看作者 |坛友微信交流群
  1. /* Summation Function with Functional Parameters
  2.    Functional Programming Example - Higher Order
  3.    H. Conrad Cunningham, Professor
  4.    Computer and Information Science
  5.    University of Mississippi

  6.    Developed for CSci 555, Functional Programming, Spring 2016

  7. 1234567890123456789012345678901234567890123456789012345678901234567890

  8. 2016-02-10: Developed from 2015 Elixir version

  9. I adapted this summation module from an Elixir version, which as, in
  10. turn, adapted from a Lua version, which was, in turn, adapted from the
  11. Scheme code in section 1.2.1 of Abelson and Sussman's Structure and
  12. Interpretation of Computer Programs (SICP).

  13. I have only tested this minimally.

  14. Scala and functional programming highlights:
  15. - Illustrates generalization of first-order functions into
  16.   higher-order
  17. - Explicitly defined return type as Double
  18. - Uses anonymous functions
  19. - Defines val to have function value

  20. */

  21. object Summation {

  22.   /* Function "sumIntegers" computes the sum of the integers in the
  23.      range from "a" through "b".
  24.   */

  25.   def sumIntegers(a: Int, b: Int): Int =
  26.     if (a > b)
  27.       0
  28.     else
  29.       a + sumIntegers(a+1,b)

  30.   /* Function "sumCubes" computes the sum of the cubes of the
  31.      integers in the range from "a" through "b".
  32.   */

  33.   def sumCubes(a: Int, b: Int): Double = {
  34.     val cube = ((x: Int) => x * x * x)
  35.     if (a > b)
  36.       0
  37.     else
  38.       cube(a) + sumCubes(a+1,b)
  39.   }
  40.   
  41.   /* Function "piSum" computes the sum of the series of terms
  42.      1/(i*(i+2)) from i = a until i > b.
  43.    
  44.      For a = 1 and b = 10 this would be 1/1*3 + 1/5*7 + 1/9*11.
  45.      For initial a = 1, this converges slowly on PI/8.
  46.   */

  47.   def piSum(a: Int, b: Int): Double =
  48.     if (a > b)
  49.       0.0
  50.     else
  51.       1.0 / (a * (a+2)) + piSum(a+4,b)


  52.   /* The above all satisfy the following template of the following
  53.      form for some NAME, TERM, and NEXT.

  54.        def NAME(a: Int, b: Int): Double =
  55.          if (a < b) 0 else TERM(a) + NAME(NEXT(a),b)

  56.     We can express this function as a higher-order function (for NAME)
  57.     with functional arguments for the operations TERM and NEXT.

  58.     Function "sum" adds the values of the application of function
  59.     "term" to integer i for all integers i in the range [1,b], where
  60.     the difference from i to its successor j is next(i).

  61.     Note: I explicitly defined the return type to be Double to avoid
  62.     complicating the function with generics and extra parameters.

  63.   */
  64.   
  65.   def sum(term: Int => Double, a: Int, next: Int => Int, b: Int): Double=
  66.     if (a > b)
  67.       0.0
  68.     else
  69.       term(a) + sum(term,next(a),next,b)
  70.       
  71.   // sumIntegers in terms of sum
  72.   def sumIntegers2(a: Int, b: Int) =
  73.     sum(((x:Int) => x), a: Int, ((x:Int) => x+1), b:Int)

  74.   // sumCubes in terms of sum
  75.   def sumCubes2(a: Int, b: Int): Double = {
  76.     def cube(x: Int): Double = x * x * x
  77.     def incr(x: Int): Int    = x+1
  78.     sum(cube,a,incr,b)
  79.   }

  80.   // piSum in terms of sum
  81.   def piSum2(a: Int, b: Int): Double = {
  82.     val piTerm = ((x: Int) => 1.0 / (x * (x+2)))
  83.     def piNext(x: Int): Int    = x + 4
  84.     sum(piTerm,a,piNext,b)
  85.   }

  86.   /* Function "sum" can be further generalized in various ways.

  87.      Question: How would we need to modify "sum" so that the resulting
  88.      higher-order function is capable of either summing or multiplying
  89.      the integers over a range?
  90.   */

  91.   // Should implement extensive tesing externally
  92.   def main(args: Array[String]) {
  93.     println("sumIntegers(1,10)  = " + sumIntegers(1,10))
  94.     println("sumIntegers2(1,10) = " + sumIntegers2(1,10))
  95.     println("sumCubes(1,5)      = " + sumCubes(1,5))
  96.     println("sumCubes2(1,5)     = " + sumCubes2(1,5))
  97.     println("piSum(1,10)        = " + piSum(1,10))
  98.     println("piSum2(1,10)       = " + piSum2(1,10))
  99.   }

  100. }
复制代码

使用道具

板凳
ReneeBK 发表于 2016-8-20 01:04:24 |只看作者 |坛友微信交流群
  1. /* Functional Programming Example -- Greatest Common Divisor
  2.    H. Conrad Cunningham, Professor
  3.    Computer and Information Science
  4.    University of Mississippi

  5.    Developed for CSci 555, Functional Programming, Spring 2016

  6. 1234567890123456789012345678901234567890123456789012345678901234567890

  7. 2016-02-10: Developed from 2015 Elixir version

  8. I adapted this greatest common divisor (gcd) module from an Elixir
  9. version, which as, in turn, adapted from a Lua version, which was, in
  10. turn, adapted from the Scheme code in section 1.2.1 of Abelson and
  11. Sussman's Structure and Interpretation of Computer Programs (SICP).

  12. I have only tested this minimally.

  13. Scala and functional programming highlights:
  14. - States complexity measures

  15. */

  16. object GCD {

  17.   /* Function "gcd" computes the greatest common divisor of its two
  18.      nonegative number arguments using Euclid's algorithm.  It is
  19.      based on property: If r is the remainder of a divided by b, then
  20.      the common divisors of a and b are precisely the same as the
  21.      common divisors of b and r.
  22.   
  23.      Time complexity:  O(log max(a,b))
  24.      Space complexity: O(1) with tail call optimization
  25.   */

  26.   def gcd(a: Int, b: Int): Int = b match {
  27.     case 0 => a
  28.     case n => gcd(b, a % b)   // tail recursion
  29.   }

  30.   // Should implement extensive tesing externally
  31.   def main(args: Array[String]) {
  32.     println("gcd(20,15) is " + gcd(20,15))
  33.     println("gcd(15,20) is " + gcd(15,20))
  34.     println("gcd(39,42) is " + gcd(39,42))
  35.     println("gcd(13,19) is " + gcd(13,19))
  36.   }

  37. }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-8-20 01:05:40 |只看作者 |坛友微信交流群
  1. /* Derivative Function with Function Return
  2.    Functional Programming Example -- Higher Order
  3.    H. Conrad Cunningham, Professor
  4.    Computer and Information Science
  5.    University of Mississippi

  6.    Developed for CSci 555, Functional Programming, Spring 2016

  7. 1234567890123456789012345678901234567890123456789012345678901234567890

  8. 2016-02-10: Developed from 2015 Elixir version

  9. I adapted this derivative module from an Elixir version, which was, in
  10. turn, adapted from a Lua version, which was, in turn, adapted from the
  11. Scheme code in section 1.2.1 of Abelson and Sussman's Structure and
  12. Interpretation of Computer Programs (SICP).

  13. I have only tested this minimally.

  14. Scala and functional programming highlights:
  15. - Takes a function argument
  16. - Returns a function
  17. - Uses anonymous function

  18. */

  19. object Derivative {

  20.   /* Function "deriv" takes a single-argument function "f" and a small
  21.      delta "dx" and returns a single-argument function (closure) that
  22.      computes an approximation of the value of the derivative at the
  23.      value of its argument.

  24.      Definition of derivative: lim(dx->0) (f(x+dx) - f(x)) / dx
  25.    */

  26.   def deriv(f: Double => Double, dx: Double): Double => Double =
  27.     ((x: Double) => (f(x+dx) - f(x)) / dx)

  28.   // Should implement extensive tesing externally
  29.   def main(args: Array[String]) {
  30.     def cube(x: Double) = x * x * x
  31.     val dx              = 0.001
  32.     val derivCube       = deriv(cube,dx)
  33.     def actualDerivCube(x: Double) = 3 * x * x

  34.     println("deriv(cube,dx)(3.0)  = " + deriv(cube,dx)(3.0))
  35.     println("actualDerivCube(3.0) = " + actualDerivCube(3.0))
  36.   }

  37. }
复制代码

使用道具

地板
ReneeBK 发表于 2016-8-20 01:07:29 |只看作者 |坛友微信交流群
  1. # Functional Programming Example -- Factorial
  2. # H. Conrad Cunningham, Professor
  3. # Computer and Information Science
  4. # University of Mississippi

  5. # Developed for CSci 556, Multiparadigm Programming, Spring 2015

  6. # 34567890123456789012345678901234567890123456789012345678901234567890

  7. # 2015-02-07: Developed from 2013 Lua version

  8. # This factorial module is adapted from a Lua version, which itself
  9. # is adapted from the Scheme code in section 1.2.1 of Abelson and
  10. # Sussman's Structure and Interpretation of Computer Programs (SICP)

  11. defmodule Factorial do
  12.   
  13.   # Function "factorial" computes the factorial for nonnegative number
  14.   # "n" using backward linear recursion. That is, the single recursive
  15.   # call needed for each level is embedded in a larger expression
  16.   # whose computation must be completed upon return from the recursive
  17.   # call.
  18.   
  19.   # Time complexity:  O(n) recursive calls of factorial
  20.   # Space complexity: O(n) active calls each taking a frame

  21.   def factorial(0)            do 1 end
  22.   def factorial(n) when n > 0 do n * factorial(n-1) end

  23.   # Function "factorial2" computes the factorial for nonnegative
  24.   # number "n" using a tail recursive auxiliary function with an
  25.   # accumulating parameter.  Tail recursion is forward linear
  26.   # recursion. Auxiliary function "fact_iter" is nested inside
  27.   # "factorial2" and can only be called from "factorial2".
  28.   
  29.   # Time complexity:  O(n) recursive calls of fact_iter
  30.   # Space complexity: O(1) with tail call optimization

  31.   def factorial2(n) when is_integer(n) and n >= 0 do
  32.     fact_iter(n,1)
  33.   end

  34.   # private tail recursive auxiliary function with accumulating
  35.   # parameter (accumulator)
  36.   
  37.   defp fact_iter(0,r) do r end
  38.   defp fact_iter(n,r) do fact_iter(n-1,n*r) end

  39. end
复制代码

使用道具

7
ReneeBK 发表于 2016-8-20 01:08:29 |只看作者 |坛友微信交流群
  1. # Functional Programming Example -- Fibonacci
  2. # H. Conrad Cunningham, Professor
  3. # Computer and Information Science
  4. # University of Mississippi

  5. # Developed for CSci 556, Multiparadigm Programming, Spring 2015

  6. # 34567890123456789012345678901234567890123456789012345678901234567890

  7. # 2015-02-08: Developed from 2013 Lua version

  8. # The functions in this Fibonacci number module are adapted from Lua
  9. # versions, which themselves are adapted from the Scheme code in
  10. # section 1.2.4 of Abelson and Sussman's Structure and Interpretation
  11. # of Computer Programs (SICP) textbook.

  12. defmodule Fibonacci do

  13.   def fib(0) do 0 end
  14.   def fib(1) do 1 end
  15.   def fib(n) when is_integer(n) and n >= 2 do
  16.     fib(n-1) + fib(n-2) # double (tree) recursion
  17.   end

  18.   # Function "fib2" takes nonnegative number "n" and computes the nth
  19.   # (second order) Fibonacci number.  It uses "fib_iter" a tail
  20.   # recursive auxiliary function with two accumulating parameters.
  21.   
  22.   # Time complexity:   O(n) recursive calls
  23.   # Space complextity: O(n); O(1) with tail call optimization

  24.   def fib2(n) when is_integer(n) and n >= 0 do fib_iter(1,0,n) end

  25.   # Tail recursive function using accumulating parameters a and b
  26.   defp fib_iter(_,b,0) do b end
  27.   defp fib_iter(a,b,m) do fib_iter(a+b,a,m-1) end

  28. end
复制代码

使用道具

8
ReneeBK 发表于 2016-8-20 01:09:57 |只看作者 |坛友微信交流群
  1. # Functional Programming Example #  Exponentiation
  2. # H. Conrad Cunningham, Professor
  3. # Computer and Information Science
  4. # University of Mississippi

  5. # Developed for CSci 556, Multiparadigm Programming, Spring 2015

  6. # 34567890123456789012345678901234567890123456789012345678901234567890

  7. # 2015-02-07: Developed from 2013 Lua version

  8. # This exponentiation module is adapted from a Lua version, which
  9. # itself is adapted from the Scheme code in section 1.2.4 of Abelson
  10. # and Sussman's Structure and Interpretation of Computer Programs
  11. # (SICP)

  12. defmodule Expt do

  13.   # Function "expt1" computes b^n (b raised to power n) using backward
  14.   # linear recursion.

  15.   # Time complexity:  O(n) recursive calls
  16.   # Space complexity: O(n) active recursive calls

  17.   def expt1(b,n) when is_number(b) and is_integer(n) and n >= 0 do
  18.     _expt1(b,n)
  19.   end
  20.   
  21.   # private auxiliary to avoid repeated type checks
  22.   defp _expt1(_,0) do 1 end
  23.   defp _expt1(b,n) do b * _expt1(b,n-1) end


  24.   # Function "expt2" computes b^n using tail recursion.

  25.   # Time complexity:  O(n) recursive calls
  26.   # Space complexity: O(1) with tail call optimization

  27.   def expt2(b,n) when is_number(b) and is_integer(n) and n >= 0 do
  28.     _expt2(b,n,1)
  29.   end

  30.   # private tail recursive auxiliary (called expt_inter in SICP)
  31.   defp _expt2(_,0,p) do p end
  32.   defp _expt2(b,n,p) do _expt2(b,n-1,b*p) end


  33.   # Fuction "expt3" computes b^n using a logarithmic algorithm and
  34.   # backward recursion.  It takes advantage of squaring.
  35.   #
  36.   #   b^n = (b^(n/2)) * (b^(n/2)) if n even
  37.   #   b^n = b * b^(n-1)           if n odd

  38.   # Time complexity:  O(log n) recursive calls
  39.   # Space complexity: O(log n) active recursive calls needing
  40.         #                   stack frames

  41.   def expt3(b,n) when is_number(b) and is_integer(n) and n >= 0 do
  42.      _expt3(b,n)
  43.   end

  44.   defp _expt3(_,0) do 1 end

  45.   defp _expt3(b,n) do
  46.     if rem(n,2) == 0  do  #  i.e. even
  47.       exp = _expt3(b,div(n,2))
  48.       exp * exp           #  backward recursion
  49.     else                  # i.e. odd
  50.       b * _expt3(b,n-1)   #  backward recursion
  51.     end
  52.   end

  53. end
复制代码

使用道具

9
twfxxc 发表于 2016-8-20 01:32:34 |只看作者 |坛友微信交流群
多谢楼主

使用道具

10
snow_boy 发表于 2016-8-20 21:12:57 |只看作者 |坛友微信交流群
顶顶DING

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-8 10:05