test n = (sum s)*4+2*(n-1)*((l')^2)-2*(n-1)*(l')
where
p =sort $[ m | a<-[1..(n-1)],b<-[1..(n-1)],let m= (b/a), (a^2+b^2< (n^2))]
l'= length p
s = (product a) : next a (group p)
where
a = [(n-1),2*(l')+(n-2)]
next [x,y] (z:z':zs) = case (null zs) of
False -> (x')*(y')*l : next [x',y'] (z':zs)
True -> (x')*(y')*l :[]
where
l = length z
x' = x+l
y' = y-length z'
Why does the above code give the following error:
No instance for (Fractional Int) arising from a use of ‘/’ In the expression: (b / a)
In an equation for ‘m’: m = (b / a)
In the second argument of ‘($)’, namely
‘[m | a <- [1 .. (n - 1)], b <- [1 .. (n - 1)], let m = (b / a), (a ^ 2 + b ^ 2 < (n ^ 2))]’
But when I substituted n=3
in the code by hand:
test' = (sum s)*4+2*(3-1)*((l')^2)-2*(3-1)*(l')
where
p = sort $[ m | a<-[1..(3-1)],b<-[1..(3-1)],let m= (b/a), (a^2+b^2< (3^2))]
l'= length p
s = (product a) : next a (group p)
where
a = [(3-1),2*(l')+(3-2)]
next [x,y] (z:z':zs) = case (null zs) of
False -> (x')*(y')*l : next [x',y'] (z':zs)
True -> (x')*(y')*l :[]
where
l = length z
x' = x+l
y' = y-length z'
Then it can be run in GHCi; what is happening?
(\x -> e) e'
can be given typet
, thene[e'/x]
(the term where we replacex
withe'
everywhere ine
) can also be given typet
. However, the reverse is generally not true:e[e'/x]
may have more types than(\x -> e) e'
. In particular whene'
is polymorphic -- as3
is in your example -- but is forced to be monomorphic by function application. – Daniel Wagner