标签: ocaml

从 Ocaml 中的嵌套条件中删除重复的语句

作为课堂练习,我们应该根据年龄和性别计算夜总会人员的入场费。25 岁以下可享受 20% 折扣,女性/NB 可享受 50% 折扣,乘数叠加叠加。

虽然我的代码可以工作,但它会重复性别检查两次,这是一种糟糕的形式,可能会在更复杂的应用程序中导致问题。怎样才能避免重复呢?

(* OCaml Version *)
let entry_price age gender =
    if age < 18 
    then (failwith "Must be over 18 to enter")
    else let price = 12.0 in
      if age <= 25
      then let price = (price *. 0.8) in
        if gender == 'f' || gender == 'x'
        then (price *. 0.5)
        else prix
      else if gender == 'f' || gender == 'x'
        then (price *. 0.5)
        else price;;

Run Code Online (Sandbox Code Playgroud)

这是一个不会重复的 Python …

ocaml

0
推荐指数
1
解决办法
83
查看次数

OCaml:运行“dune init exec”后出现沙丘构建错误

我是 OCaml 新手,我按照教程搭建了开发环境。然后我尝试使用沙丘和 OCaml 的helloworld示例,但遇到以下错误:

\n
$ dune init exe helloworld\nSuccess: initialized executable component named helloworld\n$ dune build\nError: I cannot find the root of the current workspace/project.\nIf you would like to create a new dune project, you can type:\n\n    dune init project NAME\n\nOtherwise, please make sure to run dune inside an existing project or\nworkspace. For more information about how dune identifies the root of the\ncurrent workspace/project, please refer to\nhttps://dune.readthedocs.io/en/stable/usage.html#finding-the-root\n
Run Code Online (Sandbox Code Playgroud)\n

目录结构:

\n
.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _build\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 log\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 …
Run Code Online (Sandbox Code Playgroud)

ocaml dune

0
推荐指数
1
解决办法
1820
查看次数

如何将常规列表变成选项列表

let rec some_none list =
  match list with
  | [] -> list
  | hd::tl -> 
      if hd = 0 then 
        [None] @ some_none tl 
      else
        [Some hd] @ some_none tl;;                                      
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时它返回

Error: This expression has type int list but an expression was expected of type
     'a option list
   Type int is not compatible with type 'a option 
Run Code Online (Sandbox Code Playgroud)

我怎样才能将常规更改a' lista' option list

ocaml

0
推荐指数
1
解决办法
343
查看次数

递归类型和列表有什么区别?

我对 OCaml 还很陌生(目前在大学上的课程很糟糕),我们最近研究了递归类型。我们被告知我们用它来创建列表,但 OCaml 中已经有列表了,所以我不太明白什么时候应该使用一个而不是另一个。

\n

示例\xe2\x80\xaf:

\n
(* list made with a recursive type : *)\ntype int_list =\n    | None\n    | Element of int * int_list\n\n(* just a list *)\nlet int_list2 : int list = [] \n
Run Code Online (Sandbox Code Playgroud)\n

ocaml list

0
推荐指数
1
解决办法
111
查看次数

Ocaml 中的输入和“nonrec”

我有一个令人费解的例子,下面的“ok”,我完全不明白。

module File1_intf = struct
  type type1 = { nat : int }

  module type Intf = sig
    type nonrec type1 = type1
  end
end

module File1 : File1_intf.Intf = struct
  include File1_intf
end

module File3 = struct
  open File1

  let ok : type1 -> type1 = fun { nat } -> { nat = 0 }
  let ko { nat (*Unbound record field nat*) } = { nat = 0 }
end
Run Code Online (Sandbox Code Playgroud)

我期望“未绑定记录字段”,但我想知道为什么添加类型装饰会将字段“nat”纳入范围。

ocaml

0
推荐指数
1
解决办法
198
查看次数

OCaml:访问 Stdlib 中的类型

我想使用 OCaml Stdlib 中的 Stack 模块,但我还需要一个隶属函数,而 Stack 模块缺少该函数。因此,按照https://dev.realworldocaml.org/files-modules-and-programs.html,我创建了一个文件stack1.ml

include Stack
let mem a t = List.mem a t.c
Run Code Online (Sandbox Code Playgroud)

因为是(该文件包含行:)c中使用的记录的名称。但我得到:stack.mltype 'a t = { mutable c : 'a list; mutable len : int; }

Error: Unbound record field c
Run Code Online (Sandbox Code Playgroud)

我能做些什么 ?(另外:我需要一个不同名称的文件吗Stack1?这对于调用它的文件来说有点烦人。)

ocaml

0
推荐指数
1
解决办法
76
查看次数

不确定如何读取 OCaml 函数类型签名

有人可以向我解释以下类型吗?具体来说就是返回类型。

string list -> (string * string * string) list 
Run Code Online (Sandbox Code Playgroud)

string ocaml list

0
推荐指数
1
解决办法
151
查看次数

如何使用递归类型

我需要一个函数来递归地对树中的所有数字求和。

所以我定义:

  1. 树类型,我将其定义为 type tree = [] | Intlist of int list | Treelist of tree list;;

  2. 求和函数,例如

let rec sum_list list = 
  match list with  
    Treelist (head::tail) -> (sum_list head) + (sum_list tail) 
  | Intlist (head::tail)  -> head + (sum_list tail) 
  | []                    -> 0
Run Code Online (Sandbox Code Playgroud)

我尝试编译时遇到的错误是这样的:

错误:此表达式具有树列表类型,但表达式应为树类型

它指的是 Treelist 子句中的第二个 sumlist。对我来说,树列表类型的表达式似乎应该是树类型。

怎么了?函数还是我对树的定义?

ocaml

0
推荐指数
1
解决办法
151
查看次数

如何在 OCaml 中抛出失败异常时包含整数

我有这个功能

let f = function
| 1 -> "a"
| 2 -> "b"
| _ -> failwith "Argument should be less than 3 and more than 0 but it was found to be x"
Run Code Online (Sandbox Code Playgroud)

如何将此处的值设置x为等于函数的输入?

ocaml exception pattern-matching

0
推荐指数
1
解决办法
123
查看次数

为什么函数模式匹配不起作用,而显式模式匹配却起作用?

我编写了一个函数,它接受一个数字,并返回该数字的负数版本。如果传递的数字为负数,则仅返回该数字。make_negative :: int -> int

我的第一个实现如下:

let make_negative x =
  match x with
  | 0 -> 0
  | y when y < 0 -> y
  | y when y > 0 -> -y
Run Code Online (Sandbox Code Playgroud)

当我看到这个模式时,我想我可以用以下模式替换它:

let make_negative_two =
  | 0 -> 0
  | x when x < 0 -> x
  | x when x > 0 -> -x;;
Run Code Online (Sandbox Code Playgroud)

但是,我看到以下错误:syntax error: expecting expr.

ocaml

0
推荐指数
1
解决办法
115
查看次数

标签 统计

ocaml ×10

list ×2

dune ×1

exception ×1

pattern-matching ×1

string ×1