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

Non-termination makes perfect sense, but would you mind sharing a layman explanation of what you mean by the effects of partiality?


Roughly speaking, partial functions are functions that might crash -- or that might throw exceptions, if you prefer. So even if it always terminates, it might to do so at the cost of not returning a value of the expected type.

A bit more precisely, a partial function is a function that is not defined for some values of its domain (its input). A well-known instance is the division operator, which is not defined when the divisor is zero. Other common examples are head and last:

    -- this promises to receive a list of elements of some
    type t and return the first element
    head :: [t] -> t
    head (first:rest) = first 
    head [] = error "that list has no head, yo!"
Where error is analogous to throwing an unchecked Throwable (eg RuntimeException) in Java.

One solution is to use a safer version that returns Maybe a instead of a. This is analogous to using checked exceptions.

    safeHead :: [a] -> Maybe a 
    safeHead (first:rest) = Just first
    safeHead [] = Nothing
The solution above is common and idiomatic.

Another option is to accept only arguments of a different list type that's guaranteed to be non-empty.




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

Search: