How do I go about writing a SQL-like full join operation on lists with the below signature?
fullJoin :: [a] -> [b] -> (a -> b -> Bool) -> [(Maybe a, Maybe b)]
fullJoin xs [] _ = map (\x -> (Just x, Nothing)) xs
fullJoin [] ys _ = map (\y -> (Nothing, Just y)) ys
fullJoin xs@(xh:xt) ys@(yh:yt) on =
if on xh yh
then (Just xh, Just yh) : fullJoin xt yt
else ???
The condition as written is provided by the a -> b -> Bool. here in the example it's set as (\n i -> length n == i), meaning the records from names which have the same length as the numbers in nums.
Example:
names = ["Foo","Bar", "John" , "Emily", "Connor"]
nums = [1,2,3,4,5]
fullJoin names nums (\n i -> length n == i)
== [ (Just "Foo", Just 3)
, (Just "Bar", Just 3)
, (Just "John", Just 4)
, (Just "Emily", Just 5)
, (Just "Connor", Nothing)
, (Nothing, Just 1)
, (Nothing, Just 2)
]
To clarify the exact SQL equivalent of the said function, here's how it would be written in PostgreSQL:
create table names as
select * from (values
('Foo'),
('Bar'),
('John'),
('Emily'),
('Connor')
)
as z(name);
create table nums as
select * from (values
(1),
(2),
(3),
(4),
(5)
)
as z(num);
select * from names
full join nums
on char_length(name) = num
And running this will yield:
name num
(null) 1
(null) 2
Foo 3
Bar 3
John 4
Emily 5
Connor (null)
Fiddle link : sqlfiddle