简体   繁体   English

OCaml:使用一个范围来构建列表,该范围的大小ID由从float转换为int的整数确定

[英]OCaml: building a list using a range in which the size id determined by a int cast from a float

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: 我正在尝试在OCaml中构建一个列表,该列表从返回从float转换为int的函数的函数中获取其结束范围变量:

    #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 | 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: 以及(略少但仍很易读)core_kernel List构造函数:

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. 我在想limit num的返回值被困在提供Float.to_int函数的(邪恶)单子内部。 This also happens when using Float.int_of_float, the type signatures are the same: 当使用Float.int_of_float时,类型签名也相同:

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? 那么...如何将我的int从单子中“取出”,或者,如果这不是问题,那是怎么回事,以及如何转换为可以这种方式使用的实际int

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. 更新:该错误不是由任何单调行为引起的(或根本没有使用),这是由于不正确地使用了infix运算符(mod)以及我对功能思维中的其他一些怪癖造成的,而我对此并不完全理解。 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...? 我不确定这篇文章是否应该仍然存在,但是也许这是您进入功能范式时可能犯的错误的一个例子……?

I have managed to get to the source of the problem. 我设法找到问题的根源。 Over thinking and a late night: 经过深思熟虑和深夜:

The error was not caused by any monadic behaviour (or use of monads 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. 该错误不是由任何单子行为引起的(或者根本就没有使用单子行为)引起的,这是由于错误地使用了infix运算符(mod)和功能性思考中的其他一些怪癖引起的,而我对此并不完全理解。 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...? 我不确定这篇文章是否应该仍然存在,但是也许这是您进入功能范式时可能犯的错误的一个例子……?

Credit to helping me fix the issue to Str for helping me bend my mind. 感谢帮助我解决问题的Str帮助我改变主意。 Joe Gob has also pointed out the main issue in the code. Joe Gob还指出了代码中的主要问题。 More casting between float and int based of the types required by my functions. 根据我的函数所需的类型,在float和int之间进行更多转换。

This is the corrected code (deps.ml is a requirements file): 这是更正的代码(deps.ml是需求文件):

   #use "deps.ml";;
open Batteries
open Core_kernel

let factor_of num fact = num mod fact == 0 ;;

let limit num = 2 * Float.to_int (floor (sqrt num)) ;;

let factor_list_of num =
  [? List: x | x <- 1--(limit (Int.to_float num)) ; factor_of num x ?]

let sum_factors num = ( List.fold_right (+) (factor_list_of num) 0 )

let is_perfect num = sum_factors num == num
let is_abundant num = sum_factors num > num
let is_deficient num = sum_factors num < num

Basically I have been spoiled by Ruby, but I strongly advise rubyists to take up ocaml, it's has the closest style to ruby code of the functional languages. 基本上,我已经被Ruby宠坏了,但是我强烈建议rubyists接受ocaml,它的风格与功能语言的ruby代码最接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM