lunduniversity / introprog

Teaching material for "Introduction to Programming using Scala" at Lund University, LTH. http://cs.lth.se/pgk/
142 stars 177 forks source link

prefer/include constructor pattern with explicit sequence not just x +: xs [w06] 6.1.11 #681

Closed valterbergstrand closed 2 years ago

valterbergstrand commented 2 years ago

Maybe change g +: gs to Vector(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.

bjornregnell commented 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...

valterbergstrand commented 2 years ago

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

bjornregnell commented 2 years ago

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.

valterbergstrand commented 2 years ago

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"
valterbergstrand commented 2 years ago

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*)`
bjornregnell commented 2 years ago

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 somg +: gs` ". Would that work?

bjornregnell commented 2 years ago

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...

bjornregnell commented 2 years ago

(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)

valterbergstrand commented 2 years ago

Ok, I will try to make a PR :)

bjornregnell commented 2 years ago

fixed by #682