## Haskell error when trying to get the last element

So, I am trying to implement a function that, given a list of 3 elements ((Int,Int),Int), returns True when on the second position is the same value for all 3 and False otherwise.

For instance, [((1,2),7),((5,3),7),((1,9),7)] should return True and [((1,2),3),((5,3),3),((1,9),5)] should return False.

Here is my code:

bc :: [((Int,Int),Int)]
wcheck :: [((Int,Int),Int)] -> Bool
wcheck bc
| (( fst(fst bc) == fst(snd bc) ) && ( fst(snd bc) == fst(last bc) )) = True
| otherwise                                                           = False

And the error that I get:

E:\\study related\Module1\week 4\ttt.hs:55:65: error:
* Couldn't match expected type `[(a, b0)]'
with actual type `((a, b), (a, b1))'
* In the first argument of `last', namely `bc'
In the first argument of `fst', namely `(last bc)'
In the second argument of `(==)', namely `fst (last bc)'
* Relevant bindings include
bc :: ((a, b), (a, b1))
(bound at E:\\study related\Module1\week 4\ttt.hs:54:8)
wcheck :: ((a, b), (a, b1)) -> Bool
(bound at E:\\study related\Module1\week 4\ttt.hs:54:1)
|
55 |  | (( fst(fst bc) == fst(snd bc) ) && ( fst(snd bc) == fst(last bc) )) = True
|

Could you please tell me why I get this error and a way to fix it? Thank you.

It will probably be easier if we perform simple pattern matching instead of using fst :: (a, b) -> a, etc.

##### Second item of the first item of the tuple

We can use a pattern ((_, x), _) to obtain the second element from such 2-tuple wrapped in a 2-tuple.

So we can use pattern matching like:

wcheck :: [((Int,Int),Int)] -> Bool
wcheck [((_, x), _), ((_, y), _), ((_, z), _)] = x == y && y == z
wcheck _ = False

So here if the list contains three elements, we unpack the elements, and then check if the "second item"s are equal to each other. In case the pattern does not match (for a list with too few, or too much elements), we just return False.

But a "list of three elements" does not make much sense. In case the number of elements is known at compile time, it is better to use a tuple, since in that way, the compiler can verify that you can only provide 3-tuples to that function.

##### Second item of the tuple

In case we are interested in the second item of the tuple, we can use (_, x) as pattern (we are not interested in the first item whatsoever):

wcheck :: [((Int,Int),Int)] -> Bool
wcheck [(_, x), (_, y), (_, z)] = x == y && y == z
wcheck _ = False

Note that we can generalize the signature with:

wcheck :: Eq c => [((a, b), c)] -> Bool
wcheck [(_, x), (_, y), (_, z)] = x == y && y == z
wcheck _ = False

[Haskell-beginners] my own last function, It means a list with only one element a. If someone passes in a list, it either has one element, or it has more than one and we want to get to the last one last The first question wants me to write my own last function and I wrote this doesn'​t work and it gives this error : > > Program error: pattern match  Second to last element of a list in Haskell. Ask Question. 4. Consider the following function for finding the second-to-last element of a list: myButLast (x:xs) = if length xs > 1 then myButLast xs else x. This is an O(n^2) algorithm, because length xs is O(n) and is called O(n) times.

fst and snd are not very nice in this situation. We can extract the bit we care about by doing a pattern match:

let (_,y) = x in ...

Now you want to do that to each element of your list (to check that every element has the same second value):

map (\(_,x) -> x)

And then you want to check that they are all equal:

allEqual :: Eq a => [a] -> Bool
allEqual [] = True
allEqual (x:xs) = all (\y->x==y) xs

This gets the first element, x, from a list (if it exists) and checks that ever other item y satisfies the test that x==y

So we can now write your function:

wcheck xs = allEqual (map (\(_,y) -> y) xs)

Programming in Haskell, second equation to be replaced by , but this would make the definition invalid, the empty list whereas it currently produces an error, because can match any By definition,however, removing the last element from a list with one element  I am trying to teach myself Haskell. One thing I have tried to do is to write an alternative function to return the last element of a list by recursing through the list until the tail is the empty

Given that you want to test the equality of the second element of the outer pair, there are so many ways to do this, including the following.

First, fmapping snd gives you these elements:

λ> fmap snd [((1,2),7),((5,3),7),((1,9),7)]
[7,7,7]

Now you can group them to lists of consecutive equal numbers:

λ> import Data.List
λ> (group . fmap snd) [((1,2),7),((5,3),7),((1,9),7)]
[[7,7,7]]

The values are equal if the length of this list of lists is at most one (assuming an empty list of such pairs is defined to have equal second elements):

λ> (length . group . fmap snd) [((1,2),7),((5,3),7),((1,9),7)]
1

Putting these together we can define

import Data.List

equalSnds :: Eq a => [(b, a)] -> Bool
equalSnds xs = (length . group . fmap snd) xs <= 1

So that

λ> equalSnds [((1,2),7),((5,3),7),((1,9),7)]
True
λ> equalSnds [((1,2),3),((5,3),3),((1,9),5)]
False

If you want to also test for the length of the list, you can do it separately:

wcheck :: [((Int,Int),Int)] -> Bool
wcheck xs = length xs == 3 && equalSnds xs

Haskell from the Very Beginning, Prelude> col <interactive>:4:1: error: • No instance for (Show Colour) arising from a tells Haskell to try to work out automatically (to derive) how to make Colour an (Colour a) Now you can see why getting at the last element 80 Chapter 10. "From there I'm not really sure what that's supposed to do." I'm trying to cut the list till the nth element and then take the last one (i.e. nth element). I figured out this would be the easiest way to take the nth element without !!. If you know something simpler feel free to share it. Anyways, thanks for the response! – user43051 Apr 12

Getting Started with Haskell Data Analysis: Put your data analysis , If you didn't get a File Not Found error at this point, then that means you have I'll be honest: I don't know why the CSV library does this, but the last element in  you forgot a snd I think, unless you're using some special last. adding in a snd . in front gives the type signature you're expecting, but trying out, say, takeR 3 [1..1000] gives [997,998,999,1000]. Using dropWhile and not takeWhile produces the expected result:

Functional Programming For Dummies, actual division operator is defined for many operations in Haskell; you use `div` instead. Trying to use something like avg = \x -> sum(x) / length(x) will produce an To start with, you can obtain a particular element using an index enclosed in of the list, which is [2,3,4,5,6] in this case » a[:-1]: Obtains all but the last element  It's a tradeoff. The symbol they chose is probably the most alarming symbol they could have. So I think the idea was to allows it for edge cases, but make it stand out as non-idiomatic. – cdosborn Sep 26 '16 at 20:04

Errors and Exceptions in Haskell, A basic example of an error in Haskell is trying to get the head of an A similar example is the fromJust function, which extracts the element out  The problem then becomes that you wanted the first element – the insight here is that the position of the element in the list is part of the condition you want to filter on. Therefore, you need to somehow encode the position of the element in a way that allows maximumBy to try to optimise for that as well. As it turns out, that is as simple