Reputation: 1500
I have a function triple_count
which computes the sum of 3 integers. I am attempting to use this function to construct a new function which takes an input list of integers and returns a list of sums.
Originally I tried:
triples :: [Int] -> [Int]
triples xs
| length xs < 3 = []
| otherwise = triple_count (take 3 xs) : triples (drop 3 xs)
Which fails because triple count's signature is Int -> Int -> Int -> [Int]
and not [Int] -> [Int]
.
Thanks to SO and Hoogle I was able to make this work using !!
.
triples :: [Int] -> [Int]
triples xs
| length xs < 3 = []
| otherwise = (triple_count p1 p2 p3) : triples (drop 3 xs)
where params = take 3 xs
p1 = params !! 0
p2 = params !! 1
p3 = params !! 2
Is there a better/more general way to split a list into individual parameters other than calling them out individually?
Upvotes: 2
Views: 150
Reputation: 77951
Is there a better/more general way to split a list into individual parameters other than calling them out individually?
A more readable one could be:
triples :: [Int] -> [Int]
triples (a:b:c:rest) = triple_count a b c: triples rest
triples _ = []
Upvotes: 4