haskell - Get the minimum value -
i have function 'getmin' should return minimum value. function uses function 'urvalfun' in order determine minimim value. here demonstration of 'urvalfun'
get1:: (string,string,double ) -> string get1 (x,_,_ ) = x get2 :: (string,string,double)-> string get2 (_,x,_) = x get3:: (string,string,double)->double get3 (_,_,x) = x distdiff:: string-> string->[(string,string,double)] ->double distdiff b dimat = sum [z |(x,y,z)<- dimat, (x == && y /= b) || (y == && x /= b) ] urvalfun:: int -> (string,string,double)->[(string,string,double) ]->double urvalfun size triple dimat = ((fromintegral size)-2)*(get3 triple) - ( (distdiff (get1 triple) (get2 triple) dimat ) + (distdiff (get2 triple) (get1 triple) dimat ))
its not necessary understand chunck of code, thing important if evaluate:
urvalfun 3 ("a","b",0.304) [("a","b",0.304),("a","d",0.52),("a","e",0.824)]
this evaluate -1.03999
urvalfun 3 ("a","d",0.52) [("a","b",0.304),("a","d",0.52),("a","e",0.824)]
this evaluate - 0.60799
urvalfun 3 ("a","e",0.824) [("a","b",0.304),("a","d",0.52),("a","e",0.824)]
this evaluate 1.1e^-16
now know calling urvalfun ("a","b",0.304) , [("a","b",0.304),("a","d",0.52),("a","e",0.824)] value smallest. want create function 'getmin' return minimum value (with same vector in above examples parameter) ,as shown above. problem won't work have no clue why
getmin:: [(string,string,double)]->double getmin dimat = inner 0 dimat 2000 inner 3 dimat min = min inner n dimat min |current > min = inner (n + 1) (dimatminus ++ [(head dimat) ]) min |otherwise = inner (n+1) (dimatminus ++ [(head dimat) ]) current current = urvalfun (length dimat) (head dimat) dimat dimatminus = tail dimat
try evaluate example
getmin [("a","e",0.824),("a","d",0.52),("a","b",0.304)]
which evaluate -1.1e^-16
which not want intended because want return -1.03999
could me out here?
(this code little bit ad hoc under construction, doing tests right now)
notice have rearranged vector triple ("a","b",0.304) last element in vector.
first need way minimum element pass urvalfun
. can done minimumby
.
observe following
λ> let ls = [("a","d",0.52),("a","e",0.824),("a","b",0.304)] λ> let min = minimumby (\(_,_,c) (_,_,c') -> compare c c') ls λ> urvalfun 3 min ls -1.0399999999999998
or maybe intended:
λ> minimum [ urvalfun 3 x ls | x <- ls] -1.0399999999999998
if want alter n 0 3 or something, can further modified. i'd suggest stating in english want function do.
Comments
Post a Comment