In my beginning functional programming class, I'm trying to write a program to return the middle third of a given list. As far as I know, the most straightforward way to do this would be take the first two thirds of the list, then drop the unwanted first third, returning only the middle third. However, I just can't seem to get the syntax right.
middlethird :: [a] -> [a]
middlethird l
|len `mod` 3 /= 0 = []
|otherwise = drop (1 `div`take ((2 `div` 3) len) l drop ((1 `div` 3)*len) l
where len = length l
Now I'm getting a parse error on 'where', which I don't understand... but even before that I was just getting an empty set for every list that I input, when it should only return an empty list when the length of the list isn't evenly divisible by 3.
I'm quite confused... any help or pointers would be greatly appreciated. As I mentioned, this is part of a homework assignment, but it's the very last part, and everything else seems to be running fine. Sorry for my inexperience!
EDIT: Nevermind, I figured it out.
middlethird :: [a] -> [a]
middlethird l
|mod len 3 /= 0 = []
|otherwise = drop (div len 3) (take (2*(div len 3)) l)
where len = length l