Haskell - Lisp-like symbol functionality -


i wrote lisp function while matched nested lists of symbols against other lists. worked this:

(match '(a b c) '(1 2 3))   => ((a 1) (b 2) (c 3)) (match '(a b (c d)) '(1 (2 3) (4 5)))   => ((a 1) (b (2 3)) (c 4) (d 5)) 

i trying achieve similar functionality in haskell. looking allows readable symbols used in indexing results. use integers, lists involved tended bit unreadable that. there easy symbol type in haskell?

i have come across data.atom.simple, want less awkward that.

i don't have knowledge lisp don't know if setting variables or creating associative array.

you - set variables:

main data.tree> let (node ((node b []):c:[])) = node 'a' [node 'b' [],node 'c' []] *main data.tree> 'a' *main data.tree> b 'b' *main data.tree> c node {rootlabel = 'c', subforest = []} 

but i wouldn't recommend it - matching on lists unsafe , far knowledge concerned don't know "safe" pattern matching result in maybe x.

if want have associative array, can use function this

aux :: ord k => tree k -> tree v -> map k v aux b = fromlist (go b)   go :: tree k -> tree v -> [(k,v)]         go (node k kk) (node v vv) = (k,v):(concat $ zipwith go kk vv) 

note: function works if trees have same structure

atree = node 'a' [node 'b' [],node 'c' []] btree = node 1 [node 2 [],node 3 []] ctree = node 1 [node 2 [],node 3 [], node 4 []]  ghci> aux atree btree  fromlist [('a','1'),('b','2'),('c','3')] ghci> aux atree ctree  fromlist [('a','1'),('b','2'),('c','3')] 

so see function cuts node 4 [] off.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -