The Artima Developer Community
Sponsored Link

Programming in Scala Forum
pdf page 88 - 2nd edition

2 replies on 1 page. Most recent reply: Dec 21, 2010 10:07 PM by Jim Balter

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Douglas Viltrakis

Posts: 10
Nickname: douglasv
Registered: Aug, 2010

pdf page 88 - 2nd edition Posted: Dec 21, 2010 10:46 AM
Reply to this message Reply
Advertisement
1)-def formatArgs(args: Array[String] = args.mkString("\n")
didn't work, type mismatch

2)- def formatArgs(args: Array[String]) = Array(args.mkString("\n")
did work
3) - assert(res == "zero\none\ntwo")
didn't work, not calling anything
4) - assert(res(0) == "zero\none\ntwo")
did work , calling first element, the string


George Berger

Posts: 24
Nickname: gcb
Registered: Jul, 2007

Re: pdf page 88 - 2nd edition Posted: Dec 21, 2010 11:56 AM
Reply to this message Reply
There's a typo in your item 1:

def formatArgs(args: Array[String] = args.mkString("\n")

should be:

def formatArgs(args: Array[String]) = args.mkString("\n")

I cut-and-pasted the second statement from page 97 of the pdf.

That corrected version seems to work okay in the scala interpreter:

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java 1.5.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def formatArgs(args: Array[String]) = args.mkString("\n")
formatArgs: (args: Array[String])String

scala> val res = formatArgs(Array("zero", "one", "two"))
res: String =
zero
one
two

scala> assert(res == "zero\none\ntwo")

scala>

Jim Balter

Posts: 13
Nickname: jibal
Registered: Dec, 2010

Re: pdf page 88 - 2nd edition Posted: Dec 21, 2010 10:07 PM
Reply to this message Reply
Douglas, you really should take more care in your submissions. It's page 98, not 88, and the code there is correct, whereas your (1) and (2) have syntax errors, and your (2) puts the resulting String in an Array, which is totally bogus, so it's no surprise that your (3) doesn't work.

Flat View: This topic has 2 replies on 1 page
Topic: Chapter 18 simulator Previous Topic   Next Topic Topic: Exercises in the second edition?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use