The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
Zang MingJie <zealot0... @gmail.com>
Date: Mon, 8 Oct 2012 01:16:59 -0700 (PDT)
Local: Mon, Oct 8 2012 4:16 am
Subject: week1的第三题体现出函数式语言的优美
递归+一个mapreduce就搞定了
def countChange(money: Int, coins: List[Int]): Int = if (coins.isEmpty) {
if (money == 0) 1 else 0
} else {
val cur = coins.head
(0 /: ((0 to (money / cur)) map { i => countChange(money - i * cur, coins.tail) })) { _ + _ }
}
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
颜发明 <yfam... @gmail.com>
Date: Wed, 10 Oct 2012 00:37:00 -0700 (PDT)
Subject: Re: week1的第三题体现出函数式语言的优美
我在这题上悲催了。绞尽脑汁也想不出一个好的递归方式,最后不伦不类地拼凑了一个既有递归又有while循环和var变量的怪异混合体,结果没问题,但是很丑陋 的说。 经由这一道一道的题目,慢慢地体会函数式的思想,现在找到一点点感觉了,小小进步中
在 2012年10月8日星期一UTC+8下午4时16分59秒,Zang MingJie写道:
> 递归+一个mapreduce就搞定了
> def countChange(money: Int, coins: List[Int]): Int = if (coins.isEmpty) { > if (money == 0) 1 else 0 > } else { > val cur = coins.head > (0 /: ((0 to (money / cur)) map { i => countChange(money - i * cur, > coins.tail) })) { _ + _ } > }
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Cheng CHI <cloudr... @gmail.com>
Date: Thu, 18 Oct 2012 12:40:43 -0700 (PDT)
Local: Thurs, Oct 18 2012 3:40 pm
Subject: Re: week1的第三题体现出函数式语言的优美
最后的else里这样就可以了
不过前边也有一点点点不同
(不直接发全部代码了@。@
countChange(money, coins.tail) + countChange(money-coins.head, coins)
On Monday, October 8, 2012 7:16:59 PM UTC+11, Zang MingJie wrote:
> 递归+一个mapreduce就搞定了
> def countChange(money: Int, coins: List[Int]): Int = if (coins.isEmpty) {
> if (money == 0) 1 else 0
> } else {
> val cur = coins.head
> (0 /: ((0 to (money / cur)) map { i => countChange(money - i * cur, > coins.tail) })) { _ + _ }
> }
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Lurga Lee <lurga.... @gmail.com>
Date: Fri, 19 Oct 2012 09:32:08 +0800
Local: Thurs, Oct 18 2012 9:32 pm
Subject: Re: [ScalaCN] Re: week1的第三题体现出函数式语言的优美
这不是尾递归,在实际运行时很危险。昨天翻到个不错的文章【1】,十五章到十九章很好的解释了老师不停强调的递归和list(2,3,4,5课都能看到它的影子 ) 【1】http://buaawhl.iteye.com/blog/1160327 2012/10/19 Cheng CHI <cloudr... @gmail.com>
> 最后的else里这样就可以了
不过前边也有一点点点不同
(不直接发全部代码了@。@
> countChange(money, coins.tail) + countChange(money-coins.head, coins)
> On Monday, October 8, 2012 7:16:59 PM UTC+11, Zang MingJie wrote:
>> 递归+一个mapreduce就搞定了
>> def countChange(money: Int, coins: List[Int]): Int = if (coins.isEmpty) >> { >> if (money == 0) 1 else 0 >> } else { >> val cur = coins.head >> (0 /: ((0 to (money / cur)) map { i => countChange(money - i * cur, >> coins.tail) })) { _ + _ } >> }
>> -- > 您收到此邮件是因为您订阅了 Google 网上论坛的Scala中文社区论坛。 > 要向此网上论坛发帖,请发送电子邮件至 scalacn@googlegroups.com。
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
杨博 <pop.a... @gmail.com>
Date: Fri, 19 Oct 2012 05:26:48 -0700 (PDT)
Local: Fri, Oct 19 2012 8:26 am
Subject: Re: week1的第三题体现出函数式语言的优美
你这代码不符合Scala编码规范(http://docs.scala-lang.org/style/method-invocation.html) :
> Finally, the use of the /: and :\ should be avoided in preference to the > more explicit foldLeftand foldRight method of Iterator. The > right-associativity of the /: can lead to extremely confusing code, at > the benefit of saving a few characters.
在 2012年10月8日星期一UTC+8下午4时16分59秒,Zang MingJie写道:
> 递归+一个mapreduce就搞定了
> def countChange(money: Int, coins: List[Int]): Int = if (coins.isEmpty) {
> if (money == 0) 1 else 0
> } else {
> val cur = coins.head
> (0 /: ((0 to (money / cur)) map { i => countChange(money - i * cur, > coins.tail) })) { _ + _ }
> }
You must
Sign in before you can post messages.
You do not have the permission required to post.