There are several problems in your code.
The error is that for does not return anything, so the inside of the loop should be pure for a side effect. Therefore, it must be of type unit. Your use of = not of the unit type, because = is actually an equality operator comparing two values and returning true or false .
So you are using the wrong operator. It looks like you are trying to "assign" x . But in ML you cannot assign "variables" because they are bound to a value when they are defined and cannot change. One way to get variability is to use a mutable cell (called a "link"): you use the ref function to create a mutable cell from an initial value; operator ! to get its value; and operator := to change the value inside.
So for example:
let fqp rho= let x = ref [] in if q > p then for i=0 to rho do x := q :: !x done; !x;;
newacct
source share