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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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”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:
Chapter 3, Section “Precedence Rules" (in my version)
“'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