小编jyn*_*678的帖子

F#获取列表,返回元组列表

我做了一些研究,发现List.zip函数接受两个列表并返回一个元组列表,但是如何将一个列表更改为一个元组列表呢?

let rec combinePair xs =
    match xs with
    | [] -> []
    | [x1] -> []
    | x1::x2::x3::xs -> [(x1, x2)]
    | x1::x2::xs -> [(x1, x2)] 
Run Code Online (Sandbox Code Playgroud)

如果列表中存在奇数个元素,则应删除最后一个元素,如果存在偶数个元素,则应将它们作为元组列表返回。例如

combinePair [x1; x2; x3; x4] = [(x1, x2); (x3, x4)]
Run Code Online (Sandbox Code Playgroud)

f# tuples list

4
推荐指数
1
解决办法
1504
查看次数

f#System.Char.ToUpper

我有一个练习,要求使用一个函数将字符串的所有字符转换为大写

System.Char.ToUpper
Run Code Online (Sandbox Code Playgroud)

首先,我将字符串更改为char数组,并将数组更改为字符列表

let x = s.ToCharArray()
List.ofArray x
Run Code Online (Sandbox Code Playgroud)

接下来,我想我会使用List.iter迭代我的列表并在每个字符上使用System.Char.ToUpper函数.

List.iter (fun z -> (System.Char.ToUpper(z)))
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我得到一个错误'表达式应该有单位,但这里有char.' 我究竟做错了什么?它是逻辑或语法的缺陷吗?

f# list toupper

3
推荐指数
1
解决办法
1438
查看次数

标签 统计

f# ×2

list ×2

toupper ×1

tuples ×1