Creating an array in Haskell -
Creating an array in Haskell -
i can build array with:
array ((0,0),(10,10)) [((x,y),(x)) | x<-[0..10], y<-[0..10]] but can't build function, not simple as:
array ((0,0),(10,10)) [((x,y),f(x)) | x<-[0..10], y<-[0..10]] what's wrong this?? specifically, how can convert "x" realfrac?
edit - nevermind, using function fromintegral(num) num instead of function (fromintegral num) num.
the problem when (x*1.5), you're forcing x fractional number — such float, double or rational — since (*) takes 2 values of same type , returns value of same type, , of course of study 1.5 fractional number.
the problem arises because can't create array indexed floating-point number, things integers, tuples of integers , on.1 mean maintain x , y integers, convert them fractional type calculate value:
array ((0,0),(10,10)) [((x,y), fromintegral x * 1.5) | x<-[0..10], y<-[0..10]] here, x , y integers, fromintegral x double.2 fromintegral convert integral type (like int, int64, or integer) num (in case, double):
fromintegral :: (integral a, num b) => -> b the end result array indexed (integer, integer), values of type double, presumably wanted in first place. :)
1 specifically, instance of ix type-class do; can see total list in documentation.
2 why integer , double picked rather other integral or fractional, due haskell's defaulting mechanism. basically, if have ambiguous numeric type in program, it's first tried integer , if doesn't work (e.g. because type has fractional, which, we've established, integer isn't), double tried instead. arbitrary, helps eliminate ambiguity otherwise crop in lot of silly places.
arrays haskell
Comments
Post a Comment