Reputation: 278
I am writing some Haskell which aims to take a list of numbers and filter out any number that isn't triangular.
main :: IO()
main = do
interact tri
tri :: String -> String
tri = unwords .
map show .
filter (isSquare (\n -> (8*n+1))) .
map (read :: String -> Int) .
words
isSquare :: (Integral a) => a -> Bool
isSquare n = (round . sqrt $ fromIntegral n) ^ 2 == n
This is what I have so far. Where I am having trouble is the filter.
I don't fully understand how I might go about actually accessing the value in that anonymous function (or, for that matter, if this is even possible in this manner). I need to test each number using this function in combination with isSquare
to determine whether the number is triangular. I understand the exercise is purely academic, I'm just trying to learn about function composition and IO mostly.
Upvotes: 0
Views: 101
Reputation: 565
Does the expression (isSquare (\n -> (8*n+1)))
type-check? Why not?
filter
has the type (a -> Bool) -> [a] -> [a]
. So its first argument needs to be a function of type (a -> Bool)
. You are almost there with this; what function do you want here, and how can you construct it with what you have? Hint: It's a single-character change.
Upvotes: 3