F# continuations goes on StackOverflowException - list

Hi guys I'm implementing an F# function that takes two lists of type : (int*float) list. These two lists have different lentgths.
The int element of the couple is an increasing code.
What I wanted to do is create a new list that will contain a couple (int*float) for each two elements of the two lists that have the same code. It's important to note that codes in lists are in increasing order.
These lists are probably a little long, like 2-3000 elements., so I tried to implement this function using continuation passing style in order to avoid StackOverflowExceptions. but sadly i failed.
This is the function, i hope you will give me any hints!
let identifiedDifference list1 list2 =
let rec produceResult (l1, l2) k =
match l1,l2 with
| [],[]
| _,[]
| [],_ -> k []
| (code,rate:float)::xs, (code2,rate2)::ys ->
if code = code2
then
produceResult (xs, ys) (fun c -> (code,Math.Abs(rate-rate2))::(k c))
elif code > code2
then produceResult (l1, ys) k
else produceResult (xs, l2) k
produceResult (list1, list2) id
I've done something wrong?

(fun c -> (code,Math.Abs(rate-rate2))::(k c))
should be
(fun c -> k ((code,Math.Abs(rate-rate2))::c))
to make it tail-recursive:
let identifiedDifference list1 list2 =
let rec produceResult (l1, l2) k =
match l1,l2 with
| [],[]
| _,[]
| [],_ -> k []
| (code,rate:float)::xs, (code2,rate2)::ys ->
if code = code2 then produceResult (xs, ys) (fun c -> k ((code,Math.Abs(rate-rate2))::c))
elif code > code2 then produceResult (l1, ys) k
else produceResult (xs, l2) k
produceResult (list1, list2) id
This will also fix your results being returned in reverse order.

The problem lies in this line
produceResult (xs, ys) (fun c -> (code,Math.Abs(rate-rate2))::(k c))
Here you invoke continuation but this call is not tail because you still need to cons (code,Math.Abs(rate-rate2)) to the result of (k c)
I guess you can build result list from the inside out and just reverse final result:
let identifiedDifference list1 list2 =
let rec produceResult (l1, l2) k =
match l1,l2 with
| [],[]
| _,[]
| [],_ -> k []
| (code,rate:float)::xs, (code2,rate2)::ys ->
if code = code2
then
produceResult (xs, ys) (fun c -> k((code,Math.Abs(rate-rate2))::c))
elif code > code2
then produceResult (l1, ys) k
else produceResult (xs, l2) k
produceResult (list1, list2) List.rev
EDIT:
after second look I think CPS is not needed here and using accumulator should do the trick:
let identifiedDifference list1 list2 =
let rec run l1 l2 acc =
match l1, l2 with
| [], _ | _, [] -> List.rev acc
| (code1, rate1 : float)::xs, (code2, rate2)::ys ->
if code1 = code2 then
run xs ys ((code1, abs (rate1 - rate2))::acc)
elif code1 > code2 then
run l1 ys acc
else
run xs l2 acc
run list1 list2 []

For an alternative answer, take a look at this: http://fssnip.net/75
The function takes a couple of sequences and returns pairs which match according to some matching function. I haven't volume-tested it.
The function is actually used in the larger snippet here: http://fssnip.net/76

Related

How to combine list of lists with list of lists

I have lists of lists and need to combine it with another list of lists.
Example inputs:
A: [[1,2],[3,4],[5,6],[7,8]]
B: [[1,2],[3,4],[5,6],[7,8]]
Example output:
[[1,2,1,2],[1,2,3,4],..,[7,8,5,6],[7,8,7,8]]
2 lists of lists with 4 lists inside both would return us a list of lists size 4*4 = 16
I've tried just recursively combine the lists, but i know it wouldn't work even if would go through.
mergeAll [[]] [[]] = [[]]
mergeAll [[]] b = b
mergeAll a [[]] = a
mergeAll xs ys = mergeAll (merge xs ys) (drop 1 ys)
merge :: [[a]] -> [[a]] -> [[a]]
merge [[]] [[]] = [[]]
merge xs [[]] = xs
merge [[]] ys = ys
merge (x:xs) (y:ys) = ((x++y):xs)
You can use a list comprehension:
[ xs ++ ys | xs <- listOfLists1, ys <- listOfLists2 ]
You may do like
Prelude> let doit = \as bs -> as >>= \a -> bs >>= \b -> pure (a ++ b)
Prelude> doit [[1,2],[3,4],[5,6],[7,8]] [[1,2],[3,4],[5,6],[7,8]]
[[1,2,1,2],[1,2,3,4],[1,2,5,6],[1,2,7,8],[3,4,1,2],[3,4,3,4],[3,4,5,6],[3,4,7,8],[5,6,1,2],[5,6,3,4],[5,6,5,6],[5,6,7,8],[7,8,1,2],[7,8,3,4],[7,8,5,6],[7,8,7,8]]
As Robin says in a comment, you can also do it like:
liftA2 (++)
I ask my self a question trying to understand why that is equivalent to:
[xs ++ ys | xs <- xss, ys <- yss]

F# Transforming function into using higher-order functions

I have this series of functions, isMember, addElem and countries:
let rec isMember x = function
| y::ys -> y=x || (isMember x ys)
| [] -> false
let addElem x ys = if isMember x ys then ys else x::ys
let rec countries = function
| [] -> []
| (c1,c2)::m -> addElem c1 (addElem c2 (countries m))
I want to rewrite countries using higher-order functions, but I'm not entirely sure how to:
My guess would be it having something to do with List.map, as I'm applying a function to each element of the list.
let countriesHigherOrder m =
List.map (fun x -> addElem x m)
Instead of using List.map, you can use List.fold with an accu that you initialize to [] and add elements to accu.
let countriesHigherOrder m =
List.fold (fun acc (c1,c2) -> addElem c1 (addElem c2 acc)) [] m
or by defining addPair:
let addPair (x, y) ys =
addElem x (addElem y ys)
let countriesHigherOrder m =
List.fold (fun acc (c1,c2) -> addPair (c1, c2) acc) [] m
If you want to flatten a list of pairs into a simple list and at the same time, preserve only one occurence of identical elements, the shortest code will involve the append operator.
let countries' m =
List.unzip m ||> (#)
|> Seq.distinct
|> Seq.toList
If, on the other hand, you need the peculiar order of your doubly recursive approach, you can convert the list of tuples into two-element lists and concatenate those.
let countries'' m =
List.rev m
|> List.collect(fun (x,y) -> [y;x])
|> Seq.distinct
|> Seq.toList
|> List.rev

How to insert a list in a list in all possible ways?

I am trying to enumerate all the possible merges of two lists.
In example inserting "bb" into "aaa" would look like
["bbaaa", "babaa", "baaba", "baaab", "abbaa", "ababa", "abaab", "aabba", "aabab", "aaabb"]
What I currently did is this
import Data.List
insert'' :: Char -> String -> [(String, String)] -> String
insert'' _ _ ([]) = []
insert'' h b ((x, y):xs) =
(x ++ [h] ++ (insert' (b, y))) ++ (insert'' h b xs)
insert' :: (String, String) -> String
insert' ([], ys) = ys
insert' (xs, ys) =
insert'' h b lists
where
h = head xs
b = tail xs
lists = zip (tails ys) (inits ys)
This returns for ("aaa", "bb")
"bbaaababaaabaababbaababaababbabababb"
a concatenated string, I tried making it a list of strings, but I just cannot wrap my head around this function. I always seems to get infinite type construction.
How could I rewrite the function, so it would return a list of strings?
An other implementation idea as in Daniel Wagners first post is to choose in each step a element from one of the lists and prepending it to the results generated by the function called with only the remaining parts of the list:
interleave :: [a] -> [a] -> [[a]]
interleave xs [] = [xs]
interleave [] ys = [ys]
interleave xs#(x : xs') ys#(y : ys') =
map (x :) (interleave xs' ys) ++ map (y :) (interleave xs ys')
For your intial example this produces:
ghci> interleave "bb" "aaa"
["bbaaa","babaa","baaba","baaab","abbaa","ababa","abaab","aabba","aabab","aaabb"]
Here is one implementation idea: for each element in the first list, we will choose (nondeterministically) a position in the second list to insert it, then recurse. For this to work, we first need a way to nondeterministically choose a position; thus:
choose :: [a] -> [([a], [a])]
choose = go [] where
go before xs = (before, xs) : case xs of
[] -> []
x:xs -> go (x:before) xs
For example:
> choose "abcd"
[("","abcd"),("a","bcd"),("ba","cd"),("cba","d"),("dcba","")]
Now we can use this tool to do the insertion:
insert :: [a] -> [a] -> [[a]]
insert [] ys = [ys]
insert (x:xs) ys = do
(before, after) <- choose ys
rest <- insert xs (reverse after)
return (before ++ [x] ++ rest)
In ghci:
> insert "ab" "cde"
["abcde","aebcd","adebc","acdeb","cabde","caebd","cadeb","dcabe","dcaeb","edcab"]
In this answer, I will give the minimal change needed to fix the code you already have (without completely rewriting your code). The first change needed is to update your type signatures to return lists of strings:
insert'' :: Char -> String -> [(String, String)] -> [String]
insert' :: (String, String) -> [String]
Now your compiler will complain that the first clause of insert' is returning a String instead of a [String], which is easily fixed:
insert' ([], ys) = [ys]
...and that the second clause of insert'' is trying to append a String to a [String] when running [h] ++ insert' (b, y). This one takes some thinking to figure out what you really meant; but my conclusion is that instead of x ++ [h] ++ insert' (b, y), you really want to run \t -> x ++ [h] ++ t for each element in insert' (b, y). Thus:
insert'' h b ((x, y):xs) =
(map (\t -> x ++ [h] ++ t) (insert' (b, y))) ++ (insert'' h b xs)
The complete final code is:
import Data.List
insert'' :: Char -> String -> [(String, String)] -> [String]
insert'' _ _ ([]) = []
insert'' h b ((x, y):xs) =
(map (\t -> x ++ [h] ++ t) (insert' (b, y))) ++ (insert'' h b xs)
insert' :: (String, String) -> [String]
insert' ([], ys) = [ys]
insert' (xs, ys) =
insert'' h b lists
where
h = head xs
b = tail xs
lists = zip (tails ys) (inits ys)
Now ghci will happily produce good answers:
> insert' ("aaa", "bb")
["bbaaa","babaa","baaba","baaab","abbaa","ababa","abaab","aabba","aabab","aaabb"]

Why is my order of concat lists wrong?

I'm trying to make an example function tail recursive.
Here is the original function:
let rec s xs ys =
match (xs, ys) with
|([],[]) -> []
|(xs, []) -> xs
|([], ys) -> ys
|(x::xs,y::ys) -> x::y::s xs ys
Below is my attempt to make it tail recursive:
let sC xs ys =
let rec sCTR xs ys acc =
match (xs, ys) with
|([],[]) -> acc
|(xs, []) -> acc#xs
|([], ys) -> acc#ys
|(x::xs,y::ys) -> sCTR xs ys acc#[x]#[y]
sCTR xs ys []
My issue is, however, that the order of the items are all wrong.
When I input the lists [1;2;3;] [7;8;] in the first function I get the result [1; 7; 2; 8; 3]
But when I input [1;2;3;] [7;8;] in the second function I get [3; 2; 8; 1; 7]
Why is the order wrong? I thought that list1#list2 would result in a new list with the order of list1 elements first and then list2 elements
You've just assumed the wrong precedence for #; what you've got is interpreted as
(sCTR xs ys acc)#[x]#[y]
but what you want is
sCTR xs ys (acc#[x]#[y])

Cartesian product of infinite lists in Haskell

I have a function for finite lists
> kart :: [a] -> [b] -> [(a,b)]
> kart xs ys = [(x,y) | x <- xs, y <- ys]
but how to implement it for infinite lists? I have heard something about Cantor and set theory.
I also found a function like
> genFromPair (e1, e2) = [x*e1 + y*e2 | x <- [0..], y <- [0..]]
But I'm not sure if it helps, because Hugs only gives out pairs without ever stopping.
Thanks for help.
Your first definition, kart xs ys = [(x,y) | x <- xs, y <- ys], is equivalent to
kart xs ys = xs >>= (\x ->
ys >>= (\y -> [(x,y)]))
where
(x:xs) >>= g = g x ++ (xs >>= g)
(x:xs) ++ ys = x : (xs ++ ys)
are sequential operations. Redefine them as alternating operations,
(x:xs) >>/ g = g x +/ (xs >>/ g)
(x:xs) +/ ys = x : (ys +/ xs)
[] +/ ys = ys
and your definition should be good to go for infinite lists as well:
kart_i xs ys = xs >>/ (\x ->
ys >>/ (\y -> [(x,y)]))
testing,
Prelude> take 20 $ kart_i [1..] [101..]
[(1,101),(2,101),(1,102),(3,101),(1,103),(2,102),(1,104),(4,101),(1,105),(2,103)
,(1,106),(3,102),(1,107),(2,104),(1,108),(5,101),(1,109),(2,105),(1,110),(3,103)]
courtesy of "The Reasoned Schemer". (see also conda, condi, conde, condu).
another way, more explicit, is to create separate sub-streams and combine them:
kart_i2 xs ys = foldr g [] [map (x,) ys | x <- xs]
where
g a b = head a : head b : g (tail a) (tail b)
this actually produces exactly the same results. But now we have more control over how we combine the sub-streams. We can be more diagonal:
kart_i3 xs ys = g [] [map (x,) ys | x <- xs]
where -- works both for finite
g [] [] = [] -- and infinite lists
g a b = concatMap (take 1) a
++ g (filter (not . null) (take 1 b ++ map (drop 1) a))
(drop 1 b)
so that now we get
Prelude> take 20 $ kart_i3 [1..] [101..]
[(1,101),(2,101),(1,102),(3,101),(2,102),(1,103),(4,101),(3,102),(2,103),(1,104)
,(5,101),(4,102),(3,103),(2,104),(1,105),(6,101),(5,102),(4,103),(3,104),(2,105)]
With some searching on SO I've also found an answer by Norman Ramsey with seemingly yet another way to generate the sequence, splitting these sub-streams into four areas - top-left tip, top row, left column, and recursively the rest. His merge there is the same as our +/ here.
Your second definition,
genFromPair (e1, e2) = [x*e1 + y*e2 | x <- [0..], y <- [0..]]
is equivalent to just
genFromPair (e1, e2) = [0*e1 + y*e2 | y <- [0..]]
Because the list [0..] is infinite there's no chance for any other value of x to come into play. This is the problem that the above definitions all try to avoid.
Prelude> let kart = (\xs ys -> [(x,y) | ls <- map (\x -> map (\y -> (x,y)) ys) xs, (x,y) <- ls])
Prelude> :t kart
kart :: [t] -> [t1] -> [(t, t1)]
Prelude> take 10 $ kart [0..] [1..]
[(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),(0,7),(0,8),(0,9),(0,10)]
Prelude> take 10 $ kart [0..] [5..10]
[(0,5),(0,6),(0,7),(0,8),(0,9),(0,10),(1,5),(1,6),(1,7),(1,8)]
you can think of the sequel as
0: (0, 0)
/ \
1: (1,0) (0,1)
/ \ / \
2: (2,0) (1, 1) (0,2)
...
Each level can be expressed by level n: [(n,0), (n-1, 1), (n-2, 2), ..., (0, n)]
Doing this to n <- [0..]
We have
cartesianProducts = [(n-m, m) | n<-[0..], m<-[0..n]]