作为课堂练习,我们应该根据年龄和性别计算夜总会人员的入场费。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 新手,我按照教程搭建了开发环境。然后我尝试使用沙丘和 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\nRun 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) 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' list为a' option list?
我对 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 = [] \nRun Code Online (Sandbox Code Playgroud)\n 我有一个令人费解的例子,下面的“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 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?这对于调用它的文件来说有点烦人。)
有人可以向我解释以下类型吗?具体来说就是返回类型。
string list -> (string * string * string) list
Run Code Online (Sandbox Code Playgroud) 我需要一个函数来递归地对树中的所有数字求和。
所以我定义:
树类型,我将其定义为
type tree = [] | Intlist of int list | Treelist of tree list;;
求和函数,例如
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。对我来说,树列表类型的表达式似乎应该是树类型。
怎么了?函数还是我对树的定义?
我有这个功能
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为等于函数的输入?
我编写了一个函数,它接受一个数字,并返回该数字的负数版本。如果传递的数字为负数,则仅返回该数字。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.