3 ms·
Since it's a Perl article, can we golf? Just as Conway shows that we can re-write the Perl, also the Haskell can be re-written. In general, manually dispatchi
by JadeNB 8y ago
Since it's a Perl article, can we golf? Just as Conway shows that we can re-write the Perl, also the Haskell can be re-written. In general, manually dispatching by pattern matching on a `Maybe` type I think indicates that you should be using `maybe`; I would want to replace
print_maybe :: Show a => Maybe a -> IO ()
print_maybe (Just m) = print m
print_maybe (Nothing) = putStrLn "nothing"
by
print_maybe = print . maybe "nothing" show
- Aearnus 8y agonot quite the same as what I wrote -- it would be more akin to: print_maybe = putStr . maybe "nothing" ((++ "\n") . show) or maybe something like: print_maybe c = c >>= (\d -> print d >> putStr "\n") but that's already too long to finish
- JadeNB 8y agoAccording to https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:print https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelu... , `print` already appends a newline, so I think that `print m` is not the same as `putStr $ show m` and hence that the refinements you suggest are unnecessary.
- b2gills 8y agoIf we are going to golf, here is a small Perl6 version: sub print-maybe ( $_ ) { say $_ // "nothing" } Or to translate your code: my &print-maybe = { say $_ // "nothing" } Of course both of the above take advantage of all Perl6 types being Maybe types already. (Think of defined-or `//` as having a slightly different slant to it than `||`.)