Friday, 15 January 2010

haskell - Passing different number of the arguments -



haskell - Passing different number of the arguments -

i have this:

(aeson.object jsonobject) -> case (hashmap.lookup "high" jsonobject, hashmap.lookup "low" jsonobject) of (just (string val), (string val2)) -> [val, val2] _ -> error "couldn't both keys"

i'd able pass "high" , "low" arguments , pattern matching on them retrieve actual values json. and, of course, number of such arguments can vary.

parsejson :: [string] -> [string] parsejson keys = (aeson.object jsonobject) -> case (?????) of (?????) -> ???? _ -> error "couldn't retrieve keys"

how can this?

try this. assume (aesonobject.jsonobject) refers case of existing case expression, it's not syntactically valid otherwise.

import control.monad ( sequence ) getstring (just (string value)) = value getstring _ = nil parsejson :: [string] -> [string] parsejson keys = case ... of (aeson.object jsonobject) -> case sequence (map (getstring . flip hashmap.lookup jsonobject) keys) of values -> values _ -> error "couldn't retrieve keys"

flip hashmap.lookup jsonobject gives lookup function key value (which maybe. composing getstring gives succeeds on string values, in same way existing inline pattern-match does.

finally, sequence on maybe monad turns [maybe string] maybe [string], returning just output if elements of input just.

haskell

No comments:

Post a Comment