Visual Studio代码中的F#interactive(带有Ionide)在Windows 10上默认具有语法高亮.我刚刚安装了一个Linux发行版,而VSCode + Ionide在交互式终端中没有显示语法高亮.
知道如何配置吗?这就是它现在的样子:
f# syntax-highlighting f#-interactive visual-studio-code ionide
谁能解释为什么 F# 似乎不喜欢模式处理表达式中的浮点数(Double 类型)?
\n\nlet intDiv x y =\n match x / y with\n | 5 -> printfn "Result was five - congrats"; 5\n | z -> printfn "Result was something else: %i" z; z\nRun Code Online (Sandbox Code Playgroud)\n\n匹配int类型:按预期工作。
let floatDiv x y =\n match x / y with\n | nan -> printfn "Homie, what did you even do?"; nan\n | infinity -> printfn "Wow, you got infinity!"; infinity\n | 5.0 -> printfn "Result was 5 - proud …Run Code Online (Sandbox Code Playgroud) 我有一组整数,我想计算max()我的流包含的整数数量.该max()方法来自Stream API.
我想要这样的事情
int count = Arrays.stream(myIntArray)
.filter(i -> i == max())
.count();
System.out.printf("Count: %d", count);
Run Code Online (Sandbox Code Playgroud)
我无法max()从我的forEach()方法中调用该方法,因为这不是Streams的功能 - 所以我该怎么做才能使它工作?
public class StringTest{
public static void main(String[] args){
String a = "Aphex";
int len = a.length();
int lenA = len/2-1;
int lenB = len/2+2;
if(len%2==0){
System.out.println("Number of letters has to be odd");
}else{
System.out.println(a.substring(lenA, lenB)) }}}
Output:
phe
Run Code Online (Sandbox Code Playgroud)
这个简单的代码可以使用任何具有奇数字母的字符串并找到三个中心字符串.我的问题是-为什么是居中的一个点从中间(下去len/2-1),然后两个景点了它不工作的其他方式(?len/2-2到len/2+1).
Java总是四舍五入吗?
我试图使用其内置的泛型类型在F#中实现一个非常简单的堆栈.从命令式范式来看,有时候很难想象如何避免可变性.
到目前为止我所拥有的是一个带有push和pop操作符的简单数据结构:
type Stack<'a> =
| Empty
| S of 'a list
with
member L.Push x =
match L with
| Empty | S ([]) -> S ([x])
| S (V) -> S (x :: V)
member L.Pop =
match L with
| Empty | S ([]) -> failwith "Error: Stack is empty"
| S (v::_) -> v
end
Run Code Online (Sandbox Code Playgroud)
我的想法是让Stack保持一个S of 'a list我们用cons ::运算符修改列表的地方,不要改变列表S,而是替换它S'.截至目前,堆栈最多只能有一个元素,并且在向元素推送时不会增长 - …
这可能是最简单的一个例子.
假设我有一个相当冗长的函数,可以提升整数x到n次幂.
let powInt = function
| x,n ->
let rec loop acc n =
match n with
| 0 -> acc
| v -> loop (acc * x) (v-1)
loop 1 n
Run Code Online (Sandbox Code Playgroud)
如果我现在想将整数列表中的每个整数提升到n次幂,我认为F#中的酷List.map函数将是要走的路:
let powIntList (xs: int list) = List.map powInt xs
Run Code Online (Sandbox Code Playgroud)
然而,在最后一个片段中,n功率缺乏作为映射中的参数 - 而int x由函数隐式提取map.
我们如何在这个例子中添加n power参数?