#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    47
    Rep Power
    0

    Simple methods in Scala


    "Write a method called squares that takes an integer as its only argument and returns a list of integers that are the squares of the integer up and including the integer argument"

    I'm new to Scala, so I'm a little confused on what to do. This is what I have so far:

    Code:
    	def squares(num: Int): List[Int] ={
    		var inc:Int = 1
    		var squares:List[Int] = List()
    		while(inc <= num){
    			var temp = inc*inc
    			squares += temp
    			//inc += 1
    		}
    		return squares
    
    	}
    From what I understand, += can be used to append to a list, but I'm getting a compiler error at that line: type mismatch; found: Inc required: String

    But my list is defined as an Int.

    I don't understand.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    That code compiles fine for me.

    Cut & paste the EXACT code you're trying to compile and the EXACT error message from the compiler.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    47
    Rep Power
    0
    Originally Posted by OmegaZero
    That code compiles fine for me.

    Cut & paste the EXACT code you're trying to compile and the EXACT error message from the compiler.
    Code:
    class Part1{
    
           //...other working methods...
    	def squares(num: Int): List[Int] ={
    		var inc:Int = 1
    		var squares:List[Int] = List()
    		while(inc <= num){
    			var temp = inc*inc
    			squares += temp
    			//inc += 1
    		}
    		return squares
    
    	}
    }
    
    object P2 {
    	
    	def main(args: Array[String]){
    		val obj = new Part1
    		var sq = List[Int]()
    		sq = obj.squares(4)
    
    	}
    }
    I'm in Eclipse, and temp's underlined:

    type mismatch; found: Int required: String
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    I have no problem building & running that code from the command line.

    What version of scala are you using?
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    47
    Rep Power
    0
    I don't remember the version number specifically, but it's the latest stable build. I loaded it a few days ago.

    Though I'm wondering if the installation went wrong or something; I'm having trouble importing Trees, too.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Ah. Try :+ instead of +. + was deprecated and if I'm not mistaken was finally removed in 2.8
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    47
    Rep Power
    0
    Code:
    	def squares(num: Int): List[Int] ={
    		var inc:Int = 1
    		var squares:List[Int] = List()
    		while(inc <= num){
    			var temp = inc*inc
    			squares :+ temp
    			inc += 1
    		}
    		return squares
    
    	}
    Compiles, but it doesn't look like there's anything in squares after the method finishes; accessing squares(1) throws an IndexOutOfBoundsException.

    Thanks for all the help so far.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    You never assign the result of "squares :+ temp" anywhere so you're returning the original (empty) list.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo