by Iceland_jack · 4y ago · 0 comments
newtype Structure = Structure String
append_ :: Structure -> Structure -> Structure
append_ (Structure c1) (Structure c2) = Structure (c1 <> c2)
empty_ :: Structure
empty_ = Structure ""
the <> operator (from Semigroup) can be reused by deriving it via the underlying String type (edit: I see this is suggested later in the tutorial). {-# Language DerivingStrategies #-}
{-# Language GeneralizedNewtypeDeriving #-}
newtype Structure = Structure String
deriving
newtype (Semigroup, Monoid)
append_ :: Structure -> Structure -> Structure
append_ = (<>)
empty_ :: Structure
empty_ = mempty
Semigroup and Monoid let us use a lot of standard vocabulary concatStructure :: [Structure] -> Structure
concatStructure list =
case list of
[] -> empty_
x : xs -> x <> concatStructure xs
becomes concatStructure :: [Structure] -> Structure
concatStructure = mconcat
or concatStructure = fold
Also in the detour about kinds they are written as * while the ecosystem is moving towards a more uniform Type name. {-# Language StandaloneKindSignatures #-}
import Data.Kind (Type)
type Tuple :: Type -> Type -> Type
data Tuple a b = Tuple a b
type Either :: Type -> Type -> Type
data Either a b = Left a | Right bAlso, I'll update the text about kinds when GHC will use Type instead of * everywhere:
$ ghci
GHCi, version 9.2.2: https://www.haskell.org/ghc/ :? for help
λ> :k (,)
(,) :: * -> * -> *
Until we get Type by default the best we can do is enable NoStarIsType
I think one of the cool things about Haskell is that there's quite a high ceiling in terms of solutions you can reach for. On many occasions when one get annoyed by something and thinks "there must be a better way", there is one.
You show how one with more knowledge and command of the language can make it do a lot of things for free, and that is very cool! But I can also see how these solutions can look a bit intimidating for people with less experience, and it's important to take this into account as well.
This is kind of a double edged sword. Gotta find the right balance.