我想在OCaml中获取列表的第一个和最后一个元素.我希望我的功能会像
'a list -> 'a * 'a
Run Code Online (Sandbox Code Playgroud)
我想做的是
let lista = [1;2;3;4;6;0];;
let rec first_last myList =
match myList with
[x] -> (List.hd lista,x)
| head::tail ->
first_last tail;;
first_last lista;;
Run Code Online (Sandbox Code Playgroud)
当然因为我把列表作为整数然后我正在做这样的语法
*int list -> int * 'a
Run Code Online (Sandbox Code Playgroud)
关键是我不知道如何为'a做这个功能.
方向是什么?
我有以下模块签名:
module type X_INT = sig val x: int end
Run Code Online (Sandbox Code Playgroud)
我如何编写一个以整数作为参数并生成X_INT类型的模块的函数?
let gen_module x = (* generates a module of type X_INT back *)???
Run Code Online (Sandbox Code Playgroud) 我希望在我的OCaml输入文件的开头有几行代码,以便记住最后一次输入的表达式,名称为it.即,我想拥有:
# 3 + 4;;
val it : int = 7
# it;;
val it : int = 7
# let foo = 42;;
val foo : int = 42
# it + 130;;
val it : int = 137
#
Run Code Online (Sandbox Code Playgroud)
但我不希望建立一个自定义的顶层或使用camlp5或任何幻想这样的.
我目前做的是(在OCaml版本4.02.3中,我不知道为什么我有那个版本;但我希望确切的版本没关系?)如下:
#directory "+compiler-libs";;
#load "/opt/src/ocaml-4.02.3/utils/warnings.cmo";;
#load "/opt/src/ocaml-4.02.3/parsing/location.cmo";;
let convert_phrase x =
match x with
| Parsetree.Ptop_def
[{Parsetree.pstr_desc = Parsetree.Pstr_eval (e, a)}] ->
Parsetree.Ptop_def
([{Parsetree.pstr_desc =
Parsetree.Pstr_value (Asttypes.Nonrecursive,
[{Parsetree.pvb_pat =
{Parsetree.ppat_desc =
Parsetree.Ppat_var …Run Code Online (Sandbox Code Playgroud) 我尝试了两个命令来加载OCaml顶级的Str Module.第一个命令给我错误"无法找到文件Str.cmo".然后我尝试使用我在顶级使用的文件和第二个命令.使用该命令,我得到了"未定义全局Str的引用".
#load "Str.cmo";;
#use "my_file.ml";;
Run Code Online (Sandbox Code Playgroud)
在OCaml顶级成功加载Str模块需要做什么.
在 windows 环境中使用 ocamlc:
type splitter = {varname:string; count:int; mean:float}
Run Code Online (Sandbox Code Playgroud)
当我编译源代码时工作正常,但是:
type splitter = {Varname:string; count:int; mean:float}
Run Code Online (Sandbox Code Playgroud)
导致编译器抱怨(没有提供太多信息)
File "splitter.ml", line 1, characters 17-24:
Error: Syntax error
Run Code Online (Sandbox Code Playgroud)
同时,第二行与 OCaml 完美配合。这种行为有充分的理由吗?我没有在网上找到任何东西。
我是 Python 新手,我想用 Python 抓取几个需要 Google 帐户登录的网站。例如,我想抓取一个网站 xxx.appspot.com,我需要登录我的 Google 帐户才能访问它,因为这数据库需要验证,我是授权人员。
当我做这样的事情时:
content=urllib.urlopen(target_url).read()
Run Code Online (Sandbox Code Playgroud)
我得到的“内容”当然只是一个登录页面。我如何实现代码以便抓取工具可以在我进行实际抓取之前登录 Google 帐户。
我正在尝试使用MetaOCaml,所以我做了以下出色的代码:
#let a = .<1>.;;
val a : int code = .<1>.
# let b = !. a;;
Error: Unbound value !.
# let b = .!a;;
Error: Syntax error
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
另外,我也尝试过!.,.!因为我在网站上都看过这两个网站,但不知道哪一个是正确的网站。
我有一个类型记录:
type record = { first : string; second : string list; third : string }
Run Code Online (Sandbox Code Playgroud)
我想用匹配从中提取数据......我该怎么做?
请告诉我.谢谢!
我有以下代码:
type 'v state = {
visited: string list;
unvisited: string list;
value: 'v;}
type ('a,'b) parser =
| Parser of ('a state -> 'b state list)
type 'a result =
| Ok of 'a
| Err
let parseString x = Ok x
let parseInt x = Ok (int_of_string x)
let custom = fun stringToSomething ->
Parser (fun { visited; unvisited; value } ->
match unvisited with
| [] -> []
| next::rest ->
(match stringToSomething next with
| Ok …Run Code Online (Sandbox Code Playgroud) 在一个模块中,我有一个用户定义的类型和一个返回字符串的递归函数.然后我想创建一个函数,它将创建该类型的对象并将其传递给函数.这是我的代码的一个简单示例:
type species =
Animal of string
| Mammal of species
| Fish of species
let rec species_to_string = function
| Animal (x) -> x
| Mammal (x) -> "Mammal (" ^ (species_to_string x) ^ ")"
| Fish (x) -> "Fish (" ^ (species_to_string x) ^ ")"
let process () =
let dog = Mammal(Animal("Dog"))
let dogstring = species_to_string dog
print_string dogstring
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译它时,我收到错误:
File "test.ml", line 13, characters 1-4:
Error: Syntax error
Run Code Online (Sandbox Code Playgroud)
其中第13行是上例中的第二行.
我的代码似乎不是问题.当我将代码更改为:
type species =
Animal of string …Run Code Online (Sandbox Code Playgroud)