I am trying to compare certain aspects of two linked lists.
The arrays both have format (assume a...p derive Eq):
linkedList1 = [(a, b, c, d), (e, f, g, h)]
linkedList2 = [(a, f, k, l), (a, b, g, m)]
What I hope to accomplish here is as follows:
I want to input an index, to be referenced in each quadruple.
For that index, I want to find out, for each quadruple in linkedList 1, how many times the value at that index is identical to the value at the same index in each of the quadruples in linkedList 2.
For example on the above linked lists, inputting the two linkedlists and the index 1 would return a value of 2 as the second value in the first quadruple of linkedList1 = the second value in the second quadruple of linkedList2.
Inputting the two linkedlists and the index 3, however, will return 0 as none of the 4th letters in the quadruples of the 1st linked list match any of the 4th letters in the suadruples of the 2nd linked list.
Any suggestions how to achieve this/get started? I've been trying to implement a recursive map function but it's been pretty choppy progress.
This is what I came up with:
howManyEqualAtIndex l1 l2 0
=> 2
howManyEqualAtIndex l1 l2 1
=> 2
howManyEqualAtIndex l1 l2 2
=> 1
howManyEqualAtIndex l1 l2 3
=> 0
l1 = [('a', 'b', 'c', 'd'), ('e', 'f', 'g', 'h')]
l2 = [('a', 'f', 'k', 'l'), ('a', 'b', 'g', 'm')]
--List comprehension checking all combinations.
--Predicate that they are equal
howManyEqualAtIndex xs ys idx = length [() | x<-xs, y<-ys, equalAt x y idx]
--Hardcoded for quads, can be extended to other n-tuples,
--or changed to list version commented out below
equalAt (a,_,_,_) (b,_,_,_) 0 = a == b
equalAt (_,a,_,_) (_,b,_,_) 1 = a == b
equalAt (_,_,a,_) (_,_,b,_) 2 = a == b
equalAt (_,_,_,a) (_,_,_,b) 3 = a == b
--equalAt :: (Eq a) => [a] -> [a] -> Int -> Bool
--equalAt xs ys idx = xs !! idx == ys !! idx
Related
I am trying to turn a list of tuples into a list of lists. For example, if I have the list [(9,1), (6,3), (4,1)], then this will turn into [[9, 6, 4],[6],[6]]. What's happening is that in the list of tuples [(a,b)], a represents a number from 0-9 and b represents the occurrence of that number, a will always be unique.
What I am trying to do it go go over the list n times, where n = maximum b in the list of tuples. Each time I go over the list, I take a and put it into a list, then decrement b by 1. If b == 0 then I just skip it.
So from my example, I take [9,6,4] and throw them into a list, then decrement the b of each of them, so now the list would look like [(9,0),(6,2),(4,0)]. Then going over again, I take [6], list of tuples now looks like [(9,0), (6,1), (4,0)]. Finally, take [6] one last time, and now ever b in the list of tuples is 0 so it is done.
I have created a a function that takes the 1st element from the list of tuples iff b is >= 1, but I don't know how I can iterate this over the updated list with all `b - 1' for each tuple.
turnIntList :: [(Integer, Integer)] -> [[Integer]]
turnIntList [] = []
turnIntList x = ([map (\(a, b) -> case (a,b) of _ | b >= 1 -> a | otherwise -> -1) x])
I have also tried creating another helper function that takes a list of tuples and will turn them into a list depending on how large b is. From the main function, I would try to send [(a, 1), (b, 1)...] to create the list, then keep track of decrementing b here until it's done. So, for this function:
pairingL :: [(Integer, Integer)] -> [Integer] -> [Integer]
pairingL ((a,b):xs) l -- = if b /= 0 then [a,b-1] else []
| null xs = if b == 1 then [a] ++ l else if b > 1 then [a] ++ l ++ pairingL [(a,b-1)] l else l
| otherwise =
if b /= 0 then [a] ++ l ++ pairingL ((a,b-1):xs) l else pairingL xs l
pairingL [(9,1), (7,2), (5,1)]
[9,7,7,5]
pairingL [(1,1), (2,1), (3,1)]
[1,2,3]
pairingL [(1,2), (2,2), (3,2)]
[1,1,2,2,3,3]
I've tried looking into unzipping the list and working with that, iteration, and repeat but I can't figure out how to get the function to go over the list multiple times and updating the list with the new b values then going again.
In conclusion, what I am trying to do is something like:
turnIntList [(9,3),(5,1),(2,1)]
[[9,5,2],[9],[9]]
turnIntList [(1,1),(2,1),(3,1),(4,1)]
[[1,2,3,4]]
turnIntList [(1,2),(2,2),(3,2)]
[[1,2,3],[1,2,3]]
turnIntList [(4,2),(6,1)]
[[4,6],[4]]
Process:
I am taking the first element from the tuples, adding them to a list, then subtracting the second element by 1. After doing this for each tuple in the list, I repeat the process until all the second elements for each tuple is 0
Important notes: again, in the list of tuples [(a,b)], a will ALWAYS be UNIQUE number from 0-9, and b >= 0
This maybe
turnIntList :: [(Integer, Integer)] -> [[Integer]]
turnIntList [] = [] -- if it doesn’t compile use [[]]
turnIntList ls = [i | (i, _) <- ls] : turnIntList [(i, n - 1) | (i, n) <- ls, n - 1 > 0]
For example, I have [1,2,3], I want to get [[1,2],[1,3],[2,3]]. Besides, in the list, [1,2] and [2,1] are assumed as same so just leave one in the result, and I also want to add a condition, like 'sum of the elements in sub-list is smaller than 4', then the result should be [[1,2]].
Any one knows how to solve this?
Use a list comprehension:
input = [1,2,3]
output = [ [x,y] | x <- input, y <- input, x < y, x + y < 4]
-- [[1,2]]
Here is a function which works for different types:
import Math.Combinat.Sets (choose)
select :: Int -> ([a] -> Bool) -> [a] -> [[a]]
select k filtering list = filter filtering (choose k list)
Examples:
>>> select 2 (\x -> sum x < 5) [1,2,3]
[[1,2],[1,3]]
>>> select 2 (not . elem "hello") ["hello", "how", "are", "you"]
[["how","are"],["how","you"],["are","you"]]
I have a function that a should run on a list. First, I have to compare the first two elements in the list with the function, then compare the third element with the result of the first comparison and so on, so that the result is just one element. I feel like I should use iterate, but I couldn't make it work. How can I do this?
combineOption :: Cell -> Cell -> Cell
combineOption 'f' 'f' = 'f'
combineOption 'e' 'e' = 'e'
combineOption _ _ = 'u'
combineRow :: [Cell] -> [Cell] -> [Cell]
combineRow [] [] = []
combineRow l k = [ combineOption (l !! i) (k !! i) | i <- [0..(length(l)-1)] ]
combineLineOptions :: [[Cell]] -> [Cell]
combineLineOptions l = iterate ... <==================
where type Cell = Char
have a function that a should run on a list. First, I have to compare the first two elements in the list with the function, then compare the third element with the result of the first comparison and so on, so that the result is just one element.
IIUC, that is exactly what foldl1 does. E.g., if "comparison" is difference, then
Prelude Data.List> let l = [1, 2, 3]
Prelude Data.List> foldl1 (-) l
-4
"compares" 1 and 2, then "compares" the result with 3, so it is 1 - 2 - 3 = -4.
I have a lists of list in Haskell. I want to get all the possibilities when taking one element from each list. What I have currently is
a = [ [1,2], [10,20,30], [-1,-2] ] -- as an example
whatIWant = [ [p,q,r] | p <- a!!0, q <- a!!1, r <- a!!2 ]
This does what I want. However, this is obviously not very good code, and I'm looking for a better way of writing the list comprehension so that no index number (0,1,2) shows up in the code... which is where I'm stuck.
How can I do this?
Using a function (which uses a list comprehension inside), my solution is
combinations :: [[a]] -> [[a]]
combinations [] = []
combinations [l] = map (\ x -> [x]) l
combinations (x:xs) = combine (combinations [x]) (combinations xs)
where combine a b = [ p ++ q | p <- a, q <- b ]
Example:
*Main> combinations [[1, 2, 3], [4, 5, 6]]
[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]
*Main> combinations [['a', 'b', 'c'], ['A', 'B', 'C'], ['1', '2']]
["aA1","aA2","aB1","aB2","aC1","aC2","bA1","bA2","bB1",...
"bB2","bC1","bC2","cA1","cA2","cB1","cB2","cC1","cC2"]
Edit: of course you can use the sequence function, as was suggested in the comments:
*Main> sequence [['a', 'b', 'c'], ['A', 'B', 'C'], ['1', '2']]
["aA1","aA2","aB1","aB2","aC1","aC2","bA1","bA2","bB1",...
"bB2","bC1","bC2","cA1","cA2","cB1","cB2","cC1","cC2"]
this is obviously not a good code
This is about the best way you can do it, given your constraint that the input is a list of lists.
If you use a different type, e.g. a triple of lists, then you can index structurally. E.g.
Prelude> let x#(a,b,c) = ( [1,2], [10,20,30], [-1,-2] )
Lets you write:
Prelude> [ (p,q,r) | p <- a , q <- b , r <- c ]
[(1,10,-1),(1,10,-2),(1,20,-1)
,(1,20,-2),(1,30,-1),(1,30,-2)
,(2,10,-1),(2,10,-2),(2,20,-1)
,(2,20,-2),(2,30,-1),(2,30,-2)]
Lesson: to avoid indexing, use a type whose structure captures the invariant you want to hold. Lift the dimension of the data into its type.
In an attempt to learn how haskell works, I created the following statements. I am trying to understand what their types are; can anybody let me know if I am the right track?
statement type
['h', 'e', 'l', 'l', 'o'] ['h', 'e', 'l', 'l', 'o'] :: [char]
[(9,8),(7,6),(5,4)] [(9,8),(7,6),(5,4)] :: [int a, int b] => [(a,b)]
if that's correct, can someone help me understand the type / function for these statements:
even x = x 'mod' 1 == 1
chart x y = [y| x <- [1..x]]
Your type signatures are not quite correct. Pay attention to capitalisation, it's [Char]; and it's Num and not int; and also wrong brackets: (Num a, Num b) => [(a, b)]. As for the rest...
$ ghci
[...]
Prelude> let even x = x `mod` 1 == 1
Prelude> :t even
even :: Integral a => a -> Bool
Prelude> let chart x y = [y| x <- [1..x]]
Prelude> :t chart
chart :: (Enum t1, Num t1) => t1 -> t -> [t]
Also, note the backticks on the mod, not quotes.
EDIT following the comments:
It seems that you also want the clarification on the meaning of the functions even and chart.
Even of x is the value of whether the remainder when you divide x by one is one
Unfortunately, the function is incorrectly written, since all integers are divisible by 1, and the remainder is never 1. The correct definition for even is
even x = x `mod` 2 == 0
Even of x is the value of whether the remainder when you divide x by two is zero
As for chart,
Chart of a value x and a number y is a list consisting of values y for each element in a list of numbers from 1 to x
so if you do chart 3 5, you will have a list of threes for each element in [1, 2, 3, 4, 5], so five threes: [3, 3, 3, 3, 3]
Types in Haskell are capitalized; yes, it matters.
['h', 'e', 'l', 'l', 'o'] :: [Char]
Correct, although [Char] and String are type synonyms, and String looks nicer. :P
[(9,8),(7,6),(5,4)] :: (Num a, Num b) => [(a,b)]
Multiple constraints are in round brackets. There's no "int" typeclass.
even x = x `mod` 1 == 1
even :: Integral a => a -> Bool
Make sure you use backticks and not single quotes; yes, it matters. In ghci use :t to check the type of mod and (==), and use :i to check the relationship between Num and Integral, and you should be able to put this together.
chart x y = [y | x <- [1..x]]
chart :: (Num a, Enum a) => a -> b -> [b]
For this, you have to know the "long name" of [a..b] is enumFromTo, as well as how to desugar list comprehensions. List comprehensions aren't hard, but there's already good descriptions in RWH, LYAH, and the Haskell 2010 Report, so I won't leave one here.