I'm trying to build a list in OCaml that takes its end range variable from a function which returns an int cast from a float:
#require "batteries"
#require "pa_comprehension"
#require "core_kernel"
open Batteries
open Core_kernel
let factor_of num fact = num mod fact == 0 ;;
let limit num = Float.to_int (floor (sqrt num) ) ;;
the population of this list is not performed due to:
Error: This expression has type int but an expression was expected of type [<
Downto | To ]
in both the (yummy) batteries list comprehension:
[? List: x | x <- 0--(limit num) ; factor_of num x ?] ;;
and the (slightly less but still quite readable) core_kernel List constructor:
List.(range 0 (limit num) |> filter ~f:( fun x -> factor_of num x) );;
I'm thinking that the return from limit num
is trapped inside the (evil) monad that provides the Float.to_int function. This also happens when using Float.int_of_float, the type signatures are the same:
utop # Float.to_int;;
- : float -> int = <fun>
So... how do I get my int
'out' of the monad, or, if this is not the problem, what is going on and how do I cast to an actual int
that is usable in this way?
Also, could someone point me to a decent 'What the hell are these things: Monads' tutorial for tiny, tiny brains? I am at my wits end with them.
UPDATE: The error was not caused by any monadic behaviour (or use at all, in fact) it was due to the incorrect use of an infix operator (mod) and a couple of other quirks in functional thinking that I have not completely understood. I'm not sure this post should still exist but maybe it is an example of the mistakes you can make when moving into the functional paradigm...?