# ML sum unification error

Posted 23 October 2016 - 11:40 AM

Hi,

I am trying to carry out approximations for e:
function eapprox(n) = 1+11!+12!+⋯+1(n−1)!, in ML.

My code looks like this:

[ fun facr(0) = 1
| facr(n) = n * facr(n-1) ;

fun sumr(x:real, n)=
if n=1 then x
else x + sumr(x/real(facr(n)) , n-1);

fun eapprox(n:real) = suma(1.0 , n); ]

I get an error unifying int and real, but I don't know how to solve this in ways other than I have tried in my code. notebook123, on 23 October 2016 - 11:38 AM, said:

Hi,

I am trying to carry out approximations for e:
function eapprox(n) = 1+11!+12!+⋯+1(n−1)!, in ML.

My code looks like this:

[ fun facr(0) = 1
| facr(n) = n * facr(n-1) ;

fun sumr(x:real, n)=
if n=1 then x
else x + sumr(x/real(facr(n)) , n-1);

fun eapprox(n:real) = suma(1.0 , n); ]

I get an error unifying int and real, but I don't know how to solve this in ways other than I have tried in my code.

sorry, should say 1+1/1!+1/2!+⋯+1/(n−1)!,

Is This A Good Question/Topic? 0

## Replies To: ML sum unification error

### #2 sepp2k Reputation: 2757
• Posts: 4,419
• Joined: 21-June 11

## Re: ML sum unification error

Posted 23 October 2016 - 11:52 AM

In the future please post the complete error message, including the location info (i.e. where in the code the error happens).

It seems like your problem is that you've declared n to be an int in the definition of eapprox, but you otherwise treat it as an int: you subtract 1 from it, not 1.0, you compare it to 1 (in fact you use = on it at all) and you pass it as an argument to facr, which takes an int - though given the name, that's kind of confusing.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }