Closed valterbergstrand closed 2 years ago
Yeah! But maybe both for completeness as people might run into the +: and :+ stuff on the internet, so good to having seen it...
PR welcome...
Ok, I will make a PR with the addition " g +: gs
kan substitueras mot Vector(g,gs*)
" in a comment after the match-body in the last case
I think i prefer to have them both on each line with Vector(g, gs*) first. With some additional explanation that the mean the same thing, if it fits on the slide.
I don't know if I follow, would this not mean that the last case is not excessable, since the pattern in the case before it will always match?
def visa(xs: Vector[Grönsak]): String = xs match
case Vector() => "tom grönsaksvektor"
case Vector(Gurka(v, true)) => s"en rutten gurka som väger $v"
case Vector(g) => s"exakt en grönsak: $g"
case Vector(g1, g2) => s"exakt två grönsaker: $g1, $g2"
case Vector(g, gs*) => s"först en $g och sedan svansen: $gs"
case g +: gs => s"först en $g och sedan svansen: $gs"
This is what I mean by my comment earlier:
def visa(xs: Vector[Grönsak]): String = xs match
case Vector() => "tom grönsaksvektor"
case Vector(Gurka(v, true)) => s"en rutten gurka som väger $v"
case Vector(g) => s"exakt en grönsak: $g"
case Vector(g1, g2) => s"exakt två grönsaker: $g1, $g2"
case g +: gs => s"först en $g och sedan svansen: $gs" //här kan `g +: gs` substitueras mot isärplockningen `Vector(g,gs*)`
Aha! You are right! Didn't think of that. So just change it to Vector(g, gs*)
and then a text comment last on the slide (after the question) that "Vector(g, gs*) kan också skrivas som
g +: gs` ". Would that work?
I prefer to have Vector(g ,gs)
in the pattern list as this is easiest to understand and most regular compared to the other patterns, and less cryptic...
(Your comment in the code will not fit on that line in slide, that's why I'd like g +: gs
as a note by the end of the slide outside the code block)
Ok, I will try to make a PR :)
fixed by #682
Maybe change
g +: gs
toVector(g,gs*)
on line 212 in introprog/slides/body/lect-w06-matching.tex?I would prefere the later of the two syntaxes, since "deconstruction" is one of the already introduced topics in this chapter.