Tuesday, 21 May 2013

val me = ? :: !

KOAN SPOILER ALERT
More syntax!  Now we have ‘::’  I’m on the train again with patchy network so I guessed iteratively to figure out this one.  The solution is:
koan("Lists reuse their tails") {
val d = Nil
val c = 3 :: d
val b = 2 :: c
val a = 1 :: b
a should be(List(1, 2, 3))
a.tail should be(b)
b.tail should be(c)
c.tail should be(d)
}

I’ve just remembered, I have a PDF of a roughcut of Programming Scala on my laptop.  A quick “find” has come up with this:
“you can prepend an element to a List using the :: method”
Chapter 3, Section “Precedence Rules" (in my version)
That makes perfect sense.  Again the syntax is not entirely intuitive, but while there is a bit more explanation as to the whys and wherefores of this symbology, I think I’ll stick with this for now.  Actually, one more tiny quote, just to help my “behind the curtain” understanding a little:
“'a' :: list
The […] expression is equivalent to list.::(a)”
Chapter 3, Section “Precedence Rules" (in my version)

UPDATE:  I updated the formatting a little on this after initial publishing. That's all.


No comments:

Post a Comment