小编Joh*_*Doe的帖子

括号更改功能签名

当我在函数定义中设置括号时,函数类型会发生变化.

我有两个函数:addition1(不带括号)和addition2(带括号).类型相同,但功能签名不同.为什么类型不同?

let addition1 a b =
  a + b
//val addition1 : a:int -> b:int -> int

let addition2(a, b) = 
  a + b
//val addition2 : a:int * b:int -> int
Run Code Online (Sandbox Code Playgroud)

f#

5
推荐指数
1
解决办法
142
查看次数

返回 Result 类型的异常

我正在尝试使用 F# 默认情况下的错误:异常。

我想使用 Result 类型处理错误,但如果我尝试返回 Exception 的特化(例如 ArgumentException)作为错误,Visual Studio 会显示“此函数接受太多参数,或者在不需要函数的上下文中使用” 。

我的来源:

let division (x: float) (y: float): Result<float, Exception> =
    if x = 0.0 then
        Error (ArgumentException ())
    else
        Ok (x / y)
Run Code Online (Sandbox Code Playgroud)

f#

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

对象初始化器+属性初始化器(从C#到F#)

我有一个Person,我希望使用属性初始值设定项初始化Name,使用构造函数初始化Age.

C#版本

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(int age)
    {
        Age = age
    }
}

var person = new Person(20) { Name = "Alex" };
Run Code Online (Sandbox Code Playgroud)

我试过F#:

尝试1:语法无效

type Person = {
    Name: string
    Age: int
} with 
    static member create (age: int): Person =
        { this with Age = age }: Person
Run Code Online (Sandbox Code Playgroud)

尝试2:语法无效

type Person =
    member val Name: string
    member val Age: int

    new(age: int) …
Run Code Online (Sandbox Code Playgroud)

oop f# class

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

使用合成获得偶数

我可以使用lambda语法获取列表中的偶数:

[1..10] |> List.filter (fun x -> x % 2 = 0)
Run Code Online (Sandbox Code Playgroud)

但是我希望得到它的构图,像这样:

[1..10] |> List.filter ((% 2) >> (= 0))
Run Code Online (Sandbox Code Playgroud)

错误:stdin(7,37):错误FS0010:表达式中出现意外的整数文字.预期')'或其他令牌.

f#

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

在Maybe列表上操作

我可以每2乘以一个列表:

(* 2) <$> [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

但我想要乘以Just的元素:

(* 2) <$> [Just 1, Nothing, Just 3]
Run Code Online (Sandbox Code Playgroud)

错误:

* Non type-variable argument in the constraint: Num (Maybe a)
  (Use FlexibleContexts to permit this)
* When checking the inferred type
    it :: forall a. (Num (Maybe a), Num a) => [Maybe a] Prelude Data.List 
Run Code Online (Sandbox Code Playgroud)

另一个尝试:

fmap (* 2) [Just 1, Nothing, Just 3]
Run Code Online (Sandbox Code Playgroud)

错误:

* Non type-variable argument in the constraint: Num (Maybe a)
  (Use FlexibleContexts to permit this)
* When checking …
Run Code Online (Sandbox Code Playgroud)

haskell functor maybe

2
推荐指数
2
解决办法
658
查看次数

与已定义的类相区别的联合

我有School类(有2个构造函数):

type School(name, antiquity) = 
    member this.Name: string = name
    member this.Antiquity: int = antiquity

    new(name) = School(name, 0)
Run Code Online (Sandbox Code Playgroud)

和建筑类型:

type Building =
| House
| School of School
Run Code Online (Sandbox Code Playgroud)

我想知道具有"knowType"功能的建筑物的类型:

let knowType building =
    match building with
    | House -> "A house!"
    | School -> "A school" // Error
Run Code Online (Sandbox Code Playgroud)

"knowType"中的错误在第二种情况下:"构造函数应用于0参数,但期望为1".

f# discriminated-union

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

标签 统计

f# ×5

class ×1

discriminated-union ×1

functor ×1

haskell ×1

maybe ×1

oop ×1