It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either. Therefore, a simpler solution would be
nextElem :: Eq a => a -> [a] -> Maybe a
nextElem e xs
| null match = Nothing
| null rest = Just (head xs)
| otherwise = Just (head rest)
where
(skip, match) = break (== e) xs
rest = tail match
What is the expected result for nextElem something [something]
— a one-element list that happens to contain the element you are looking for? I believe you would produce a nullNothing
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but If you could almost achievewant to preserve the same goal by treating xs
linearly. The only caseoriginal behavior, where the circularity makes any difference is when enextElem 'a' "a"
is the last element ofproduces xs'a'
, andthen you should be able to handle that situation without using shiftList
.could insert
nextElem e [_] = Nothing
For that matter, if you are doing anything other wrap-around operations with this list, it would make sense to make shiftList
as a public functionspecial case to this solution.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either.
What is the expected result for nextElem something [something]
— a one-element list that happens to contain the element you are looking for? I believe you would produce a null
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but you could almost achieve the same goal by treating xs
linearly. The only case where the circularity makes any difference is when e
is the last element of xs
, and you should be able to handle that situation without using shiftList
.
For that matter, if you are doing anything other wrap-around operations with this list, it would make sense to make shiftList
a public function.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either. Therefore, a simpler solution would be
nextElem :: Eq a => a -> [a] -> Maybe a
nextElem e xs
| null match = Nothing
| null rest = Just (head xs)
| otherwise = Just (head rest)
where
(skip, match) = break (== e) xs
rest = tail match
What is the expected result for nextElem something [something]
— a one-element list that happens to contain the element you are looking for? I believe you would produce Nothing
. I would have expected something
instead. If you want to preserve the original behavior, where nextElem 'a' "a"
produces 'a'
, then you could insert
nextElem e [_] = Nothing
as a special case to this solution.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either.
What is the expected result for nextElem something [something]
— a one-element circular list list that happens to contain the element you are looking for? I believe you would produce a null
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but you could almost achieve the same goal by treating xs
linearly. The only case where the circularity makes any difference is when e
is the last element of xs
, and you should be able to handle that situation without using shiftList
.
For that matter, if you are doing anything elseother wrap-around operations with this circular list, it would make sense to make shiftList
a public function.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either.
What is the expected result for nextElem something [something]
— a one-element circular list that happens to contain the element you are looking for? I believe you would produce a null
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but you could almost achieve the same goal by treating xs
linearly. The only case where the circularity makes any difference is when e
is the last element of xs
, and you should be able to handle that situation without using shiftList
.
For that matter, if you are doing anything else with this circular list, it would make sense to make shiftList
a public function.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either.
What is the expected result for nextElem something [something]
— a one-element list that happens to contain the element you are looking for? I believe you would produce a null
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but you could almost achieve the same goal by treating xs
linearly. The only case where the circularity makes any difference is when e
is the last element of xs
, and you should be able to handle that situation without using shiftList
.
For that matter, if you are doing anything other wrap-around operations with this list, it would make sense to make shiftList
a public function.
It looks like you are trying to do the same thing in two different ways.
To detect the case where e
is not in xs
, you should check whether yb
is empty. (If that's what you were trying to do with nextElem' _ (x : [])= Nothing
then you should note that it doesn't do that.) I'm not sure what the purpose of the otherwise
case is. If break
didn't find any element equal to e
, recursion won't find it, either.
What is the expected result for nextElem something [something]
— a one-element circular list that happens to contain the element you are looking for? I believe you would produce a null
. I would have expected something
instead.
What is the point of this function? It finds the answer by simulating circularity, but you could almost achieve the same goal by treating xs
linearly. The only case where the circularity makes any difference is when e
is the last element of xs
, and you should be able to handle that situation without using shiftList
.
For that matter, if you are doing anything else with this circular list, it would make sense to make shiftList
a public function.