haskell create list from lists with different attributes -


yes uni assignment question, please not give me answer, need able learn , how it, (mostly because there further questions , need develop understanding of haskell language them!

the question:

join :: eq => [(a,b)] -> [(a,c)] -> [(a,b,c)]. 

join takes 2 lists of pairs, , returns single list of triples. triple generated when there exists member of both argument lists have same first element. list elements not sorted. same semantics relational algebra natural join operation.

for example:

join [(2,"s"),(1,"j")] [(2,true),(3,false)])      should produce output [(2,"s",true)] join [(2,"s"),(1,"j")] [(2,1),(2,2),(3,4)])     should produce output [(2,"s",1),(2,"s",2)] 

my problem

my main problem trying figure out how create new list 2 lists input, have different attributes.

what have far:

join :: eq => [(a,b)] -> [(a,c)] -> [(a,b,c)] join xs     []     = xs join []     ys     = ys join (x:xs) (y:ys) = (x ++ snd(head[x]) ++ snd(head[y]) ++ []) join xs ys 

the resulting error:

type error in explicitly typed binding *** term           : merge *** type           : [(a,b)] -> [(a,c)] -> [(a,b)] *** not match : [(a,b)] -> [(a,c)] -> [(a,b,c)] 

other notes:

i have tried several variations of (x ++ snd(head[x]) ++ snd(head[y]) ++ []) section of code, results being same or simular error message!

your types not match

join :: eq => [(a,b)] -> [(a,c)] -> [(a,b,c)] join xs     []     = xs join []     ys     = ys                  --  ^^^^   3 different types involved! 

xs , ys has type [(a, b)] , [(a, c)] then, can not resultant type [(a,b,c)].

you must create resultant data xs , ys, like

join ? ? = ... (a, b, c) ... 

can figure that?

tip: body of

maketuple :: (a, b) -> (a, c) -> (a, b, c)                          --          ^  ^                          --          :  |                          --          :  +-- 1 value exists can put here                          --          :                          --          +····· 1 value exists can put here 

the 2 possible functions are

maketuple (x1, y) (x2, z) = (x1, y, z) 

and

maketuple (x1, y) (x2, z) = (x2, y, z) 

in same way, join must preserve resultant type.

from signature

join :: eq => [(a,b)] -> [(a,c)] -> [(a,b,c)] 

you know type a equatable , know nothing b , c types.

a simple way is

join xs ys = [(xa, xb, yc) | (xa, xb) <- xs, (ya, yc) <- ys, xa == ya]                                       --                     ^^^^^^^^                                       --                 `eq a` constraint 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -