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.
Related
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 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.
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.
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.