Non.exhaustive pattern [closed] - 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 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.

Related

Custom types and their usage [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 1 year ago.
Improve this question
Hello I have 3 custom types and then type created with these 3 types.
type Name = String
type Age = Int
type Semester = Int
type Student = (Name, Age, Semester)
I need to create function which takes student and returns his name
i have created this but it doesn't work.
getName :: Student -> Name:
getName (name_, age_, semester_) = name_
This works fine:
getName :: Student -> Name
getName (name_, age_, semester_) = name_
There shouldn't be any : or anything else in the end of the signature.
First line contains the signature by itself. Then go the lines with the definition. The type alias is Name, not Name:, so that's what should be used.

Haskell convert list of list of strings to string [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 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"]]

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 call a function returning a tuple in another function in haskell? [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 have two functions like these:
notice_objects_at::String -> IO()
notice_objects_at place = do
let (X,Y) = at place
putStrLn ("There is a" ++ show X ++ "," ++ show Y ++ "here.")
putStrLn "Hi"
at::String-> (String, String)
at place =
case place of
"bedroom" -> ("fly", "light switch")
"den" -> ("flyswatter", "light switch")
from the 'at' function I am returning a tuple, which I want to store to two variables X and Y in the notice_objects_at function. But, I'm getting an error that:
Not in scope: data constructor ‘X’
Not in scope: data constructor ‘Y’
Not in scope: data constructor ‘X’
Not in scope: data constructor ‘Y’
What is wrong?
Haskell syntax relies on the capitalisation of names. As described here:
Anything that starts with a capital letter is either a concrete type
or a data constructor. Lower-case-starting names are reserved for
function names and variables, including type variables.
So when you bind names to the elements of the tuple in:
let (X,Y) = at place
you need lowercase names:
let (x,y) = at place
(and adjust the names wherever else they are used, of course!)
Otherwise Haskell interprets these names as data constructors, but of course cannot find their definition anywhere, hence your error messages.
See also Why does Haskell force data constructor's first letter to be upper case?

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