lomeo: (лямбда)
Dmitry Antonyuk ([personal profile] lomeo) wrote2008-07-22 03:13 pm

Partial function

Я вот жаловался [livejournal.com profile] thesz, что в Haskell нет аналога partial function в Scala, чтобы можно было легко строить конструкции аналогично Erlang'овского receive (я говорю о синтаксисе). Однако механизмы есть. Правда, я нашёл пока только для IO.

import Prelude hiding (catch)
import Control.Exception

e `patternFail` h = e `catch` \(PatternMatchFail _) -> h

apply handlers = \x -> foldl1 patternFail (map ($x) handlers)

-- *Main> apply [\[1]->putStrLn "a", \[2]->putStrLn "b", \[3]->putStrLn "c"] [2]
-- b


Список здесь всего лишь пример, можно сделать и по другому (varargs?)

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting