trakk
trakk

Reputation: 3

Operations with a list made up of custom types

I'm pretty new to Haskell, and I'm struggling with something specific. Namely, having a list that contains a series of custom types. So far, this is where I'm at:

type shots = [(Float,Float)]

value :: shots -> [Float]
value [(a,b)] = [(10-(sqrt(a^2+b^2)))]

Now, this seems to be working, but only for one pair of float values. What I'm trying to do here is, put in a series of float pairs, calculate with the equation, then return the result in a list of floats. I understand why it doesn't take any more in its current form (because of a and b), but no matter how I try to work it into a head-tail loop, it ceases to function. Tried using fst and snd as well, with no success.

Upvotes: 0

Views: 60

Answers (2)

karakfa
karakfa

Reputation: 67557

you may want to learn the building blocks as well. For example you can write your function for a pair and then map it to a list of pairs.

f (a,b) = 10 - sqrt(a^2 + b^2)

> map f [(3,4),(5,12)]
[5.0,-3.0]

or defined within the context of your function

value = map f
   where f (a,b) = 10 - sqrt(a^2 + b^2)

where GHC can infer the type

> :t value
value :: Floating b => [(b, b)] -> [b]

Upvotes: 1

gabesoft
gabesoft

Reputation: 1228

Create a function process that does the calculation for a single pair

process (a, b) = (10-(sqrt(a^2+b^2)))

then map that function over your list of tuples

fmap process [(1,2),(3,4),(45,55)]

which will yield

[7.76393202250021,5.0,-61.06335201775947]

Upvotes: 0

Related Questions