2

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!

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.