7 ms·
Parser combinators in F# were where I really grasped the utility of a Monad. F# doesn’t have HKT’s, and computational expressions are a little different from Ha
by ddek 5y ago
Parser combinators in F# were where I really grasped the utility of a Monad. F# doesn’t have HKT’s, and computational expressions are a little different from Haskell ‘do’.
It was the ‘(>>=) = Parser<‘a> -> (‘a -> Parser<‘b>) -> Parser<‘b>’ that flicked the lightswitch in my head.
- contravariant 5y agoIn .NET I still find it weird that people sometimes implement the visitor pattern without the output type. You'd have to truly think imperatively to consider: void Structure(IVisitor visitor); a good function signature, instead of: A Structure(IParser<A> parser); I mean fair enough that you can't be bothered to implement the functor and monad methods, but it's downright silly to define a function that is incapable of returning output.
- frabert 5y agoIn .NET specifically a reason I can think about doing that is that you'd need to implement your visitor twice, as the type argument cannot be void (if I recall correctly).
- Joker_vD 5y agoIt can't be void, but writing public sealed class Unit { private static Unit m_value = new Unit(); public static Unit Value => m_value; public Unit() { } public override string ToString() => "()"; public override bool Equals(object obj) => obj is Unit; public override int GetHashCode(object obj) => 0; } and then using it ("return Unit.Value") in place of void is really not that big a problem.
- contravariant 5y agoTrue, but I just never quite managed to understand why you'd want your visitor to return nothing. And if you did why it was too much hassle to just return '0' or something.