一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1060|回复: 18
收起左侧

[CS61A]Homework02

[复制链接] |试试Instant~ |关注本帖
sky420 发表于 2015-5-6 06:05:20 | 显示全部楼层 |阅读模式

[其他]CS61A #1 - 4@UCBerkely

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

x
本帖最后由 AveMaleficum 于 2015-5-7 01:40 编辑

第二次作业,通过之前的discussion就觉得,higher-order function好弯弯绕,这次作业的challenge问题,也需要自己考虑一下,大家可以在这个帖子下面多讨论
http://gaotx.com/cs61a/hw/hw02/


补充内容 (2015-5-8 13:44):
我的作业
http://gaotx.com/blogs/2015/05/08/cs61a-hw02/

评分

1

查看全部评分

FFFelix 发表于 2015-5-8 19:56:53 | 显示全部楼层
  1. def square(x):
  2.     return x * x

  3. def triple(x):
  4.     return 3 * x

  5. def identity(x):
  6.     return x

  7. def increment(x):
  8.     return x + 1

  9. def piecewise(f, g, b):
  10.     """Returns the piecewise function h where:

  11.     h(x) = f(x) if x < b,
  12.            g(x) otherwise

  13.     >>> def negate(x):
  14.     ...     return -x
  15.     >>> abs_value = piecewise(negate, identity, 0)
  16.     >>> abs_value(6)
  17.     6
  18.     >>> abs_value(-1)
  19.     1
  20.     """
  21.     "*** YOUR CODE HERE ***"
  22.     def h(x):
  23.         if x < b:
  24.             return f(x)
  25.         else:
  26.             return g(x)
  27.     return h

  28. def repeated(f, n)
  29.     "*** YOUR CODE HERE ***"

  30.     g = f
  31.     while n > 1:
  32.         g = compose1(f,g)
  33.         n -= 1
  34.     return g

  35. def compose1(f, g):
  36.     """Return a function h, such that h(x) = f(g(x))."""
  37.     def h(x):
  38.         return f(g(x))
  39.     return h

  40. ###################
  41. # Church Numerals #
  42. ###################

  43. def zero(f):
  44.     return lambda x: x

  45. def successor(n):
  46.     return lambda f: lambda x: f(n(f)(x))

  47. def one(f):
  48.     """Church numeral 1: same as successor(zero)"""
  49.     "*** YOUR CODE HERE ***"
  50.     return lambda x: f(x)

  51. def two(f):
  52.     """Church numeral 2: same as successor(successor(zero))"""
  53.     "*** YOUR CODE HERE ***"
  54.     return lambda x: f(f(x))

  55. three = successor(two)

  56. def church_to_int(n):
  57.     "*** YOUR CODE HERE ***"
  58.     return n(lambda x : x + 1)(0)

  59. def add_church(m, n):
  60.     "*** YOUR CODE HERE ***"
  61.     return lambda f: lambda x: m(f)(n(f)(x))

  62. def mul_church(m, n):
  63.     "*** YOUR CODE HERE ***"
  64.     return lambda f: m(n(f))

  65. def pow_church(m, n):

  66.     "*** YOUR CODE HERE ***"
  67.     return n(m)
复制代码
challenge problem自己完全不知道怎么做。 看了答案以后稍稍明白了一点,至少church_to_int(n)完全明白。 但是后面三个还是有点迷迷糊糊的,不知是否有人可以讲解一下。

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-5-9 20:37:25 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
    if x < b:
        return f(x)
    else:
        return g(x)
    return h
    "*** YOUR CODE HERE ***"

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    i = 1
    g = f
    while i < n :
        g = compose1(f, g)
        i += 1
    return g   
    "*** YOUR CODE HERE ***"

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return lambda f: lambda x: f(zero(f)(x))
    "*** YOUR CODE HERE ***"

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return lambda f: lambda x: f(f(zero(f)(x))(f)(x))
    "*** YOUR CODE HERE ***"

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

geminiiiiiii 发表于 2015-5-9 20:37:56 | 显示全部楼层
geminiiiiiii 发表于 2015-5-9 20:37
def square(x):
    return x * x

第三题写一半就写不下去了。。
回复 支持 反对

使用道具 举报

mjtyumi 发表于 2015-5-10 17:05:30 | 显示全部楼层
Q1:def piecewise(f,g,b):
        def h(x):
                if x<b:
                        return f(x)
                return g(x)
        return h


Q2:
def repeated(f,n):
        def h(x):
                k=0
                while k<n:
                        x=f(x)
                        k=k+1
                return x
        return h


Q3(optional):
TAT...

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

神罗天征 发表于 2015-5-11 10:15:05 | 显示全部楼层
  1. def square(x):
  2.     return x * x

  3. def triple(x):
  4.     return 3 * x

  5. def identity(x):
  6.     return x

  7. def increment(x):
  8.     return x + 1

  9. def piecewise(f, g, b):
  10.     """Returns the piecewise function h where:

  11.     h(x) = f(x) if x < b,
  12.            g(x) otherwise

  13.     >>> def negate(x):
  14.     ...     return -x
  15.     >>> abs_value = piecewise(negate, identity, 0)
  16.     >>> abs_value(6)
  17.     6
  18.     >>> abs_value(-1)
  19.     1
  20.     """
  21.     "*** YOUR CODE HERE ***"
  22.     def h(x):
  23.         if(x<b):
  24.             return f(x)
  25.         else:
  26.             return g(x)
  27.     return h

  28. def repeated(f, n):
  29.     """Return the function that computes the nth application of f.

  30.     >>> add_three = repeated(increment, 3)
  31.     >>> add_three(5)
  32.     8
  33.     >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
  34.     243
  35.     >>> repeated(square, 2)(5) # square(square(5))
  36.     625
  37.     >>> repeated(square, 4)(5) # square(square(square(square(5))))
  38.     152587890625
  39.     """
  40.     "*** YOUR CODE HERE ***"
  41.     h=f
  42.     while(n>1):
  43.         h=composel(h,f)
  44.         n=n-1
  45.     return h
  46.         

  47. def compose1(f, g):
  48.     """Return a function h, such that h(x) = f(g(x))."""
  49.     def h(x):
  50.         return f(g(x))
  51.     return h第三个能说题都没看懂了吗?求大神解释一下<img src="http://www.1point3acres.com/bbs/static/image/smiley/QQ/em34.gif" border="0" smilieid="112" alt="">
复制代码

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

 楼主| sky420 发表于 2015-5-11 11:08:03 | 显示全部楼层

基本的意思是,用a set of functions代替integers。根据第一个function ZERO的不同,和SUCCESSOR,可以自己创建新的INTEGER。比如,根据我们自己定义,我可以把10定义成ZERO,那ONE就是11,TWO就是12。题目的要求就是,根据ZERO,和SUCCESSOR,ONE是什么?TWO是什么?任意两个INTEGER如何相加,想乘,甚至做power
回复 支持 反对

使用道具 举报

神罗天征 发表于 2015-5-11 14:18:57 | 显示全部楼层
sky420 发表于 2015-5-11 11:08
基本的意思是,用a set of functions代替integers。根据第一个function ZERO的不同,和SUCCESSOR,可以自 ...

原来是这样!谢楼主!
回复 支持 反对

使用道具 举报

karte_polo 发表于 2015-5-11 20:50:36 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x<b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if n == 1:
            return f(x)
        else:
            return f(repeated(f,n-1)(x))
    return h

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    "*** YOUR CODE HERE ***"
    def h(x):
        return f(x)
    return h

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    "*** YOUR CODE HERE ***"
    def h(x):
        return f(f(x))
    return h

   

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"
    v = n(increment)(0)
    return v

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"
    n_int = church_to_int(n)
    while n_int>0:
          m = successor(m)
          n_int-=1
    return m
        

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"
    n_int = church_to_int(n)
    t = zero
    while n_int>0:
        t = add_church(t,m)
        n_int-=1
    return t

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"
    n_int = church_to_int(n)
    t = m
    while n_int>1:
        t = mul_church(t,m)
        n_int-=1
    return t

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

go7going 发表于 2015-5-11 22:20:49 | 显示全部楼层
感觉第三题有点难 搞不懂是什么意思
先交了 回头再看。



""""

def piecewise(f, g, b):
    def key(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return key
"""""

""""
def compose1(f, g):
    def h(x):
        return f(g(x))
    return h

def repeated(f, n):
    g = f
    while n > 1:
        g = compose1(f, g)
        n = n - 1
    return g
"""""
""""
def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
     return lambda x: f(x)
def two(f):
     return lambda x: f(f(x))
three = successor(two)
def church_to_int(n):
     return n(lambda x: x + 1)(0)
def add_church(m, n):
    return lambda f: lambda x: m(f)(n(f)(x))
def mul_church(m, n):
     return lambda f: m(n(f))
def pow_church(m, n):
    return n(m)
"""""

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

goldpanda 发表于 2015-5-12 10:20:16 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    if n == 1:
        return f
    else:
        return compose1(f, repeated(f, n-1))


def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return lambda x : f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return lambda x : f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(lambda x : x+1)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    return lambda f : lambda x : m(f)(n(f)(x))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    return lambda f : m(n(f))

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    return n(m)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

LuckyBamboo 发表于 2015-5-12 11:35:56 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f
        else:
            return g
    return h


def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i = 0
        while i < n:
            x, i = f(x), k + 1
        return x
    return h



def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(x)


def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    "*** YOUR CODE HERE ***"
    return successor(two)


three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"
    return n(lambda x: x + 1)(0)



def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"
    return lambda: lambda x: m(f)(n(f)(x))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"
    return lambda f: m(n(f))


def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"
    retun n(m)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

jy_121 发表于 2015-5-12 21:32:54 | 显示全部楼层
Question1:

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
        def h(x):
            if x < b:
                return f(x)
            return g(x)
        return h
   


Question2:

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    g = f
    while n > 1:
        g = compose1(f, g)
        n = n - 1
    return g

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h



第三题真心不会,求讲解。。。

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wuxiaomin98 发表于 2015-5-23 05:51:00 | 显示全部楼层
def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    g = f
    while n > 1:
        g = compose1(f, g)
        n -= 1
    return g


def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

第三题真心看不懂successor怎么定义的

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

qiuyuxiruchou 发表于 2015-6-2 10:36:02 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h
   
def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    def h(x):
        i=0
        while i < n:
            x=f(x)
            i=i+1
        return x
    return h

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################
def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return lambda x: f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return lambda x: f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(lambda x:x+1)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    return lambda f: lambda x: m(f)((n(f)(x)))

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    return lambda f: m(n(f))

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    return lambda n(m)

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

reasonapp 发表于 2015-6-3 22:49:05 | 显示全部楼层
lambda到底是个啥,第三题跪了
#hw02
#Q1
def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def back(x):
            if x < b:
                    return f(x)
            else:
                    return g(x)
    return back
   
#Q2
def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        i = 1
        while i <= n:
            x, i = f(x), i + 1
        return x
    return h
        

Q3:
。。。。。

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

wkh279 发表于 2015-7-17 20:12:00 | 显示全部楼层
本帖最后由 wkh279 于 2015-7-17 20:13 编辑

最近才开始跟课程,贴一下代码哈
最后几问看了下答案,的确没有答案简

def square(x):
    return x * x

def triple(x):
    return 3 * x

def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    def h(x):
        if x<b: return f(x)
        else: return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    g=f
    for i in range(n-1):
        g=compose1(f,g)
    return g

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    return f

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    return compose1(f,f)

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    return n(increment)(0)

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x+y)
    return g

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x*y)
    return g

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    x,y=church_to_int(m),church_to_int(n)
    def g(f):
        return repeated(f,x**y)
    return g

评分

1

查看全部评分

回复 支持 反对

使用道具 举报

liyimeng 发表于 2015-12-30 13:17:41 | 显示全部楼层
这次那道challenge题好难阿。。现在仍旧没有理解,尤其是后面三问。。不过用取巧的方法解出来了。。
代码和图片如下,求加分!!!
hw02.png
  1. def square(x):
  2.         return x * x

  3. def triple(x):
  4.         return 3 * x

  5. def identity(x):
  6.         return x

  7. def increment(x):
  8.         return x + 1

  9. def piecewise(f, g, b):
  10.         """Returns the piecewise function h where:

  11.         h(x) = f(x) if x < b,
  12.                    g(x) otherwise

  13.         >>> def negate(x):
  14.         ...     return -x
  15.         >>> abs_value = piecewise(negate, identity, 0)
  16.         >>> abs_value(6)
  17.         6
  18.         >>> abs_value(-1)
  19.         1
  20.         """
  21.         "*** YOUR CODE HERE ***"
  22.         def compare(x):
  23.                 if x < b:
  24.                         return f(x)
  25.                 else:
  26.                         return g(x)

  27.         return compare

  28. def repeated(f, n):
  29.         """Return the function that computes the nth application of f.

  30.         >>> add_three = repeated(increment, 3)
  31.         >>> add_three(5)
  32.         8
  33.         >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
  34.         243
  35.         >>> repeated(square, 2)(5) # square(square(5))
  36.         625
  37.         >>> repeated(square, 4)(5) # square(square(square(square(5))))
  38.         152587890625
  39.         """
  40.         "*** YOUR CODE HERE ***"
  41.         i = 1
  42.         g = f
  43.         while i < n:
  44.                 g = compose1(f, g)
  45.                 i += 1

  46.         return g

  47. def compose1(f, g):
  48.         """Return a function h, such that h(x) = f(g(x))."""
  49.         def h(x):
  50.                 return f(g(x))
  51.         return h

  52. ###################
  53. # Church Numerals #
  54. ###################

  55. def zero(f):
  56.         return lambda x: x

  57. def successor(n):
  58.         return lambda f: lambda x: f(n(f)(x))

  59. def one(f):
  60.         """Church numeral 1: same as successor(zero)"""
  61.         "*** YOUR CODE HERE ***"
  62.         return lambda x: f(x)
  63.        

  64. def two(f):
  65.         """Church numeral 2: same as successor(successor(zero))"""
  66.         "*** YOUR CODE HERE ***"
  67.         return lambda x: f(f(x))

  68. three = successor(two)

  69. def church_to_int(n):
  70.         """Convert the Church numeral n to a Python integer.

  71.         >>> church_to_int(zero)
  72.         0
  73.         >>> church_to_int(one)
  74.         1
  75.         >>> church_to_int(two)
  76.         2
  77.         >>> church_to_int(three)
  78.         3
  79.         """
  80.         "*** YOUR CODE HERE ***"
  81.         return n(increment)(0)


  82. def add_church(m, n):
  83.         """Return the Church numeral for m + n, for Church numerals m and n.

  84.         >>> church_to_int(add_church(two, three))
  85.         5
  86.         """
  87.         "*** YOUR CODE HERE ***"
  88.         x = church_to_int(m)
  89.         y = church_to_int(n)
  90.         return lambda f: repeated(f, x + y)                                                                                                                                                                                               

  91. def mul_church(m, n):
  92.         """Return the Church numeral for m * n, for Church numerals m and n.

  93.         >>> four = successor(three)
  94.         >>> church_to_int(mul_church(two, three))
  95.         6
  96.         >>> church_to_int(mul_church(three, four))
  97.         12
  98.         """
  99.         "*** YOUR CODE HERE ***"
  100.         x = church_to_int(m)
  101.         y = church_to_int(n)
  102.         return lambda f: repeated(f, x * y)       

  103. def pow_church(m, n):
  104.         """Return the Church numeral m ** n, for Church numerals m and n.

  105.         >>> church_to_int(pow_church(two, three))
  106.         8
  107.         >>> church_to_int(pow_church(three, two))
  108.         9
  109.         """
  110.         "*** YOUR CODE HERE ***"
  111.         x = church_to_int(m)
  112.         y = church_to_int(n)
  113.         return lambda f: repeated(f, x ** y)       
复制代码
回复 支持 反对

使用道具 举报

Liaeve 发表于 2016-2-5 22:51:25 | 显示全部楼层
def square(x):
    return x * x

def triple(x):
    return 3 * x
第3题写到一半放弃了,lambda始终不太明白


def identity(x):
    return x

def increment(x):
    return x + 1

def piecewise(f, g, b):
    """Returns the piecewise function h where:

    h(x) = f(x) if x < b,
           g(x) otherwise

    >>> def negate(x):
    ...     return -x
    >>> abs_value = piecewise(negate, identity, 0)
    >>> abs_value(6)
    6
    >>> abs_value(-1)
    1
    """
    "*** YOUR CODE HERE ***"
    def h(x):
        if x < b:
            return f(x)
        else:
            return g(x)
    return h

def repeated(f, n):
    """Return the function that computes the nth application of f.

    >>> add_three = repeated(increment, 3)
    >>> add_three(5)
    8
    >>> repeated(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
    243
    >>> repeated(square, 2)(5) # square(square(5))
    625
    >>> repeated(square, 4)(5) # square(square(square(square(5))))
    152587890625
    """
    "*** YOUR CODE HERE ***"
    def nthf(x):
         i = 1
         result = f(x)
         while i < n:
            result = f(result)
            i = i + 1
         return result
    return nthf

def compose1(f, g):
    """Return a function h, such that h(x) = f(g(x))."""
    def h(x):
        return f(g(x))
    return h

###################
# Church Numerals #
###################

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

def one(f):
    """Church numeral 1: same as successor(zero)"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(x)

def two(f):
    """Church numeral 2: same as successor(successor(zero))"""
    "*** YOUR CODE HERE ***"
    return lambda x: f(f(x))

three = successor(two)

def church_to_int(n):
    """Convert the Church numeral n to a Python integer.

    >>> church_to_int(zero)
    0
    >>> church_to_int(one)
    1
    >>> church_to_int(two)
    2
    >>> church_to_int(three)
    3
    """
    "*** YOUR CODE HERE ***"
    return n(lambda x : x + 1)( 0 )

def add_church(m, n):
    """Return the Church numeral for m + n, for Church numerals m and n.

    >>> church_to_int(add_church(two, three))
    5
    """
    "*** YOUR CODE HERE ***"

def mul_church(m, n):
    """Return the Church numeral for m * n, for Church numerals m and n.

    >>> four = successor(three)
    >>> church_to_int(mul_church(two, three))
    6
    >>> church_to_int(mul_church(three, four))
    12
    """
    "*** YOUR CODE HERE ***"

def pow_church(m, n):
    """Return the Church numeral m ** n, for Church numerals m and n.

    >>> church_to_int(pow_church(two, three))
    8
    >>> church_to_int(pow_church(three, two))
    9
    """
    "*** YOUR CODE HERE ***"
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-8 10:37

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表