Haskell - sortBy function -


i have list of vectors. want sort list of vectors length, using sortby function. have is:

import data.list  vectorlength::(int,int)->float vectorlength(x,y) = sqrt(fromintegral ((x^2)+(y^2)))  sortvectors::[(int, int)]->[(int, int)] sortvectors list = sortby(map vectorlength list) list  main =     print(map vectorlength [(1,4), (2,6), (-2, -8), (3, -4)])     print(sortvectors[(1,4), (2,6), (-2,-8), (3, -4)]) 

the vectorlength function work.

map vectorlength [(1,4), (2,6), (-2,-8),(3,-4)]    output: [4.1231055, 6.3245554, 8.246211, 5.0] 

i want when calling following function

sortvectors [(1,4), (2,6), (-2,-8), (3,-4)]   output: [(-2,-8), (2,6), (3,-4), (1,4)] 

but following error:

couldn't match expected type `(int, int)' actual type `[a0]'     expected type: (int, int) -> (int, int) -> ordering       actual type: [a0] -> [b0]     in return type of call of `map'     in first argument of `sortby', namely `(map vectorlength list)'     in expression: sortby (map vectorlength list) list 

thank help. here solution

import data.list  vectorlength::(int,int)->float vectorlength(x,y) = sqrt(fromintegral ((x^2)+(y^2)))  sortvectors::[(int, int)]->[(int, int)] sortvectors list = rever(sortby comparevectors list)  rever::[(int, int)]->[(int, int)] rever [] = [] rever (x:xs) = rever xs ++ [x]  comparevectors::(int, int) ->(int, int) ->ordering comparevectors(a,b) (c,d)      | vectorlength(a,b) < vectorlength(c,d) = lt     | vectorlength(a,b) > vectorlength(c,d) = gt  main =     print(map vectorlength [(1,4), (2,6), (-2, -8), (3, -4)])     print(sortvectors[(1,4), (2,6), (-2,-8), (3, -4)]) 

you write:

sortby (comparing vectorlength) .... 

you gave list first element sortby, function required.

to write out, want is:

sortby comparvectors listofvectors     comparvectors b = vectorlength `compare` vectorlength b 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -