Haskell for Imperative Programmers #21 - data, type & newtype

แชร์
ฝัง

ความคิดเห็น • 6

  • @edvardm4348
    @edvardm4348 2 ปีที่แล้ว +5

    These videos were in my opinion really, really well made. I'd put these videos to the same list of definite resources to go through along Hutton's Haskell book and LYAH.
    Thank you so much.

  • @VladTrishch
    @VladTrishch 4 ปีที่แล้ว +11

    Love this series! Please, keep it up.

  • @arber10
    @arber10 2 ปีที่แล้ว

    Very good explanation. Thanks!

  • @kid-vf4lu
    @kid-vf4lu 4 ปีที่แล้ว +11

    This is fantastic content! If you start a patreon, I will contribute

  • @alexanderskusnov5119
    @alexanderskusnov5119 4 ปีที่แล้ว +3

    1) I would add a record syntax:
    data RGB = RGB {red:: Int, green:: Int, blue:: Int}
    2) Why with record syntax I see an error?:
    newtype RGB = RGB {red:: Int, green:: Int, blue:: Int}
    but this case is legal?:
    newtype RGB = RGB (Int, Int, Int)

    • @philipphagenlocher
      @philipphagenlocher  4 ปีที่แล้ว +7

      The second case doesn't work since red, green and blue are three distinct fields for the RGB constructor. Without the record syntax it would look like this: newtype RGB = RGB Int Int Int which isn't legal since the constructor has 3 fields.
      newtype RGB = RGB (Int, Int, Int) is legal since there is only one field (namely (Int, Int, Int) which is one tuple with some stuff in it).
      In general, using records in conjunction with newtype is not really helpful since only one field (and thus only one name) can be introduced to the type.