How to find time complexity of this program? [closed] - haskell

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.

Related

If length is 2 do Haskell [closed]

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.

Using value itself without the name of value in Python 3 [closed]

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 2 years ago.
Improve this question
Is there any way to use variable itself inside without it's name?
For example, I have a string like this:
someStuffVariableName = "abcdefghijklmnop..."
If I want to manipulate it, I need to write every time name of this var but it's so long:
someStuffVariableName = someStuffVariableName[0:-1]
But,anyway,can I do like this:
someStuffVariableName = self[0:-1] or someStuffVariableName = this.value[0:-1]?
There is not.
Your best options are:
Use a more concise variable name (but don't give up readability!)
Just deal with the length
Note that in some cases, the answer is actually yes. For instance, you can often write x += y instead of x = x + y, and x /= y instead of x = x / y.
But this is for assignment operators only.

How to modify haskell function to remove duplicates [closed]

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.

Haskell Reversed Number Digit List [closed]

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 8 years ago.
Improve this question
I am new to Haskell and I am trying to turn an int into a reversed digit list(of ints).
What I have is:
Lnat 0 = [0]
Lnat x = [mod x 10] ++ Lnat (div x 10)
However I get the error "Not in scope: data constructor 'Lnat'" on both lines and it crashes trying to load the file.
Could you please explain the root of this and how to fix it?
All values must start with a lowercase character. If it starts with a capital or : then that value is a data constructor, to be used in data declarations. This is what you'll want to change your function to:
lnat 0 = [0]
lnat x = mod x 10 : lnat (div x 10)
Note that I also changed the inefficient ++ operator to : to add a bit more speed.

How to return the next letter in the alphabet of given letter wrapped around in Haskell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to implement a function that returns the next letter in alphabetical order. For example:
> returnNext 'A'
'B'
But also:
> returnNext 'Z'
'A'
The function should thus cycle between char codes in alphabetical order (mod 26).
Two ways come to mind
import Data.Char
returnNext c = chr (((1 + ord c - ord 'A') `mod` 26) + ord 'A')
Which is kind of ugly to say the least.
And:
returnNext 'Z' = 'A'
returnNext c = chr (ord c + 1)
Both behave differently when not given a letter of the alphabet, but since you didn't specify what should happen in this case I'll assume it's OK.

Resources