Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is a great example of how people who use ad-hoc languages can get distorted views of how recursion should be denoted. No functional programmer in the universe would write something that looks like your recursive example.

Here are two recursive ways to write a min function:

Idiomatic and structured, using (Maybe . Min) monoid:

    min = foldMap (Just . Min)
Incidentally, the above works on any data structure where this operation makes sense, not just lists, which is nice.

Unstructured, ad-hoc

    minimum [] = Nothing
    minimum (x:xs) = case minimum xs of Nothing -> Just x; Just y -> Just (min x y)
This latter doesn’t even rely on anything unique to functional programming ecosystems (like folds or principled algebraic classes).


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: