Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a program that searches through a list of tuples (Char, Bool) and returns true if there are any two conflicting elements within it. Two tuples are conflicting if they have the same value for the first element (Char) in the tuple but the second ones are different (Bool).
For example
[('a', True),('a', False)] returns False , 'a'='a' but True != False
[('a', True),('a', True) returns True. , 'a'='a' and True=True
[('a', True),('a', True), ('b', true), ('c', false)] returns true
ListofTupels :: [(a,a)] -> [a]
type ListofTupels = [(Char,Bool)]
The function is defined like this
searchValidity :: ListofTupels -> Bool
How to do this?
Use recursion. An empty list is vacuously valid:
type ListofTuples = [(Char,Bool)]
searchValidity :: ListOfTuples -> Bool
searchValidity [] = True
An non-empty list consists of a valid tail and a head that doesn't invalidate the list.
searchValidity ((c, b):rest) = searchValidity rest && ...
I leave filling in ... as an exercise. The lookup function in the Prelude should be useful.
(I leave it as a further exercise to do this in subquadratic time. Using lookup at each step slows this down greatly.)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm new to Haskell and I'm not sure how to work around the If-Else, for example:
function str = if ((length str) = 2) then (....)
In java we would:
if (str.length =2){
str = "2"}
else { str ="1"}
How do you write it in haskell?
You can use Guards:
fnc :: String -> String
fnc s | length s == 2 = ...
| otherwise = ...
More to Guards
Or conditions
fnc :: String -> String
fnc s = if length s == 2 then ... else ...
It is also possible to use pattern matching, more here.
There are several ways to achieve conditions (e.g. case of) in Haskell.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to pretty-print a list of list of strings as a single string. My current code is
pretty :: [[String]]->String
pretty x
= concat(concat(x))
main :: IO ()
main
= putStrLn (show (pretty [["hi","hi","hi"]]))
When asked to do pretty [["hi","hi","hi"]], it converts it to "hihihi" but when asked pretty [["hi","hi","hi"]["hi","hi","hi"]] the compiler gives the following error
main.hs:7:29: error:
* Couldn't match expected type `[[Char]] -> [String]'
with actual type `[[Char]]'
* The function `["hi", "hi", "hi"]' is applied to one argument,
but its type `[[Char]]' has none
In the expression: ["hi", "hi", "hi"] ["hi", "hi", "hi"]
In the first argument of `pretty', namely
`[["hi", "hi", "hi"] ["hi", "hi", ....]]'
|
7 | = putStrLn (show (pretty [["hi","hi","hi"]["hi","hi","hi"]]))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Never mind guys. It turned out I forgot the comma in [["hi","hi","hi"],["hi","hi","hi"]]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this haskell function that flattens a two deep list into one list. How can I edit it to make sure that it doesn't allow for duplicates? An example would be
flatten[['a'],[],['a','b']] --> ['a','b']
My current program would output
['a','a','b']
flatten :: [[a]] -> [a]
flatten [] = []
flatten ([]:vs) = flatten vs
flatten ((x:xs):vs) = x:flatten (xs:vs)
Skipping over duplicates in an unsorted list requires keeping a set of already-seen elements, and checking every incoming element.
Something like:
dedupliction_step :: [a] -> SetOf a -> [a] -> [a]
deduplication_step [] _ output = output
dedupliction_step (x:rest) already_seen output =
if x `belongsTo` already_seen then deduplication_step rest already_seen output
else deduplication_step rest updated_seen x:output where
updated_seen = putElementInto already_seen x
This gives you the idea. How do you implement SetOf and its related manipulations, depends on the problem at hand.
For short sets, you can use SetOf = List; then belongsTo = elem. It has a linear lookup time, though, so for long sets it becomes expensive.
For long sets, you can use e.g. a Data.Tree or Data.Set with logarithmic lookup and update time.
For short sets of numbers, Data.Bits could be considered; it's O(1) lookup and update, but limited to 32 or 64 values.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So im creating a function that says true if a is equal to the fst element of any pair in a list
elemMSet :: Eq a => a -> [(a,Int)] -> Bool
elemMset a [] = False
elemMSet a ((t,q):xs)| a==t = True
| otherwise = elemMSet a xs
I dont undertstand why, it shows an error of non-exhaustive pattern when i try something that should give False like :
elemMSet 'd' [('b',2), ('a',4), ('c',1)]
Error:
Tseis.hs:(4,1)-(5,48): Non-exhaustive patterns in function elemMSet
You misspelled the function name on line 2, so elemMSet only covers the non-empty case. Change the name on line 2 to elemMSet (with a capital S) and it will work fine.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a data list.
db = [("Ada","works", "IBM")
,("Alice","director", "Ada")
,("Tom","works", "IBM")
,("Tommy","director", "Tom")
,("IBM","isat", "CA")
,("CA","in", "USA")
]
ask db = map (\(x,y,z) -> (z == "IBM")) db
How to calculate the complexity of O(n)?
If I want to get the result by the length of list 2,5,10.O(n) is same like 2,5,10?And If I do
trans2 db = concat (map ((x,y,z) -> concat (map((x',y',z') -> if (z==x') then [] else [(x,y ++ "." ++ y',z')] else []) db)) db )
How can I calculate the O(n)? The runtime of program? The timming complexity
The question is unclear and I expect it will soon be closed. Briefly.
O(n) is a complexity. If you know O(n) and you wanted complexity then you're done.
The length of the list (2, 5, 10, what have you) is not a factor in the complexity in this case since the length is what the n is representing.
There is no code that will calculate the complexity of the algorithm automatically. It is a manual analysis.