grajpawel
grajpawel

Reputation: 23

Taking array as an input to function in Haskell

I'm trying to learn Haskell and I stumbled upon a problem in my Haskell code. I have a function,

main = print (qSort [distance (3,4), distance (1,2), distance (2,2)])

distance :: (Floating a ) => (a,a) -> (a,a,a)
distance (x2 , y2) = (x2*x2 + y2*y2, x2, y2) 

that calculates distance between (0,0) and given point. How can change it to something like:

    main = print (qSort (distance [(3,4),(1,2),(2,2)]))

so that distance can take a whole array as input? Also, what way will be the best to try and get the points as input from the user? Looking at examples I can't really think of a way to get points. I've tried fiddling with square brackets, but I keep getting errors. Any help would be appreciated!

Upvotes: 2

Views: 375

Answers (1)

Enlico
Enlico

Reputation: 28510

Try putting map distance instead of distance in the second code.

But you have to lookup and understand what map does!

In this case, map is telling distance Hey, man, you're promoted! Yeah, you don't just work on loosers pairs, (a,a); you work on lists of them, [(a,a)], all at once!

(The technical term for promoted is lifted, btw.)

If you're really at the start, I suggest that you go though some tutorial/book. LYAH is a very good place to start.

Upvotes: 4

Related Questions