Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
week1的第三题体现出函数式语言的优美
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
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:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Zang MingJie  
View profile  
 More options Oct 8 2012, 4:16 am
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.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
颜发明  
View profile   Translate to Translated (View Original)
 More options Oct 10 2012, 3:37 am
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写道:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cheng CHI  
View profile   Translate to Translated (View Original)
 More options Oct 18 2012, 3:40 pm
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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lurga Lee  
View profile   Translate to Translated (View Original)
 More options Oct 18 2012, 9:32 pm
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>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
杨博  
View profile  
 More options Oct 19 2012, 8:26 am
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写道:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »