-- Example of the call-by-ref pattern for a Maybe type -- Sergio Antoy and Michael Hanus -- Mon Apr 18 13:07:40 PDT 2011 -- updated Wed Jul 5 10:29:55 PDT 2017 {-# OPTIONS_CYMAKE -F --pgmF=currypp --optF=defaultrules #-} import Test.EasyCheck -- The function returns True/False for success/failure to lookup a key. -- The key value, when there exists, is bound to the argument -- "passed by reference". mlookup :: a -> [(a,b)] -> b -> Bool -- mlookup k (_++[(k,x)]++_) x = True -- mlookup'default _ _ _ = False mlookup k [] _ = False mlookup k ((h,x):t) y | k==h = x=:=y | otherwise = mlookup k t y table = [("wine","good"),("beer","better"),("whiskey","careful")] -- the behavior/value returned by reference can be customized mloopup_with_fail y = if mlookup y table x then x else failed where x free mloopup_maybe y = if mlookup y table x then Just x else Nothing where x free test10 = mloopup_with_fail "beer" -=- "better" test11 = failing (mloopup_with_fail "cider") test20 = mloopup_maybe "beer" -=- Just "better" test21 = mloopup_maybe "cider" -=- Nothing