unit-type

What is () in Haskell, exactly?

假如想象 提交于 2019-12-17 21:44:29
问题 I'm reading Learn You a Haskell , and in the monad chapters, it seems to me that () is being treated as a sort of "null" for every type. When I check the type of () in GHCi, I get >> :t () () :: () which is an extremely confusing statement. It seems that () is a type all to itself. I'm confused as to how it fits into the language, and how it seems to be able to stand for any type. 回答1: tl;dr () does not add a "null" value to every type, hell no; () is a "dull" value in a type of its own: () .

Why do I need to use the unit type in F# if it supports the void type?

一笑奈何 提交于 2019-12-12 07:06:59
问题 I read this MSDN article: Unit Type (F#) ...The unit type is a type that indicates the absence of a specific value; the unit type has only a single value, which acts as a placeholder when no other value exists or is needed ... The unit type resembles the void type in languages such as C# and C++... So... Alright, I understand, that the unit type is such a type, which has only a single value () . But I have some questions: Why is it needed? When is it needed? I don't understand why not to use

F# inferred types in If/Then

非 Y 不嫁゛ 提交于 2019-12-10 19:35:57
问题 If I have the following function: let myFunc x y = if y = 0 then 1 x I get the error: Program.fs(58,17): error FS0001: This expression was expected to have type unit but here has type int Why does the compiler expect 'unit' instead of int ? 回答1: It might worth adding that this is not just a property of if . F# is an expression-based language meaning that pretty much every piece of code (aside from type declarations and a few exceptions) is an expression that evaluates to some result. In fact,

F# compiler error “This expression was expected to have type unit but here has type bool.” expression in {if else} statements

不打扰是莪最后的温柔 提交于 2019-12-10 17:14:29
问题 I have written such a function in F#: let TwistBasket (reverse: bool, quarters: int, overTwist: int byref) = overTwist <- 50 WaitForBasketReady() waitBasket.Reset() let move = 135*quarters - 25 + overTwist let speed = match reverse with | true -> -75y | false -> 75y let waitHandle = motorBasket.SpeedProfile(speed, 15u, uint32 move, 10u, true) Task.Factory.StartNew(fun () -> waitHandle.WaitOne() if (overTwist <> 0) then motorBasket.SpeedProfile(sbyte -speed, 0u, uint32 overTwist, 0u, true)

Why is `unit` treated differently by the F# type system when used as a generic interface argument?

﹥>﹥吖頭↗ 提交于 2019-12-08 17:30:10
问题 Consider this interface: type A<'a> = abstract X : 'a Let's try to implement it with int as a generic argument: { new A<int> with member this.X = 5 } // all is well Now, let's try unit for an argument: // Compiler error: The member 'get_X : unit -> unit' does not have the correct type to override the corresponding abstract method. { new A<unit> with member this.X = () } Now, if we define a non-generic interface, everything also works well: type A_int = abstract X : int { new A_int with member

What do the empty parentheses `()` mean in Elm?

谁说我不能喝 提交于 2019-12-08 15:35:21
问题 I found out they are mean an empty tuple. However, are they also used as a convention by Elm programmers to mean "value can be ignored"? is13 : Int -> Result String () is13 code = if code == 13 then Ok () else Err "not the right key code" Source: https://github.com/pdamoc/elmChallenges/blob/master/challenge5.elm 回答1: The empty parentheses () are what's known as a unit type, that is, a type that can only ever have a single value. A tuple type with at least one item can have any number of

Implement F# interface member with Unit return type in C#

*爱你&永不变心* 提交于 2019-12-04 03:56:34
问题 Suppose I've defined the following interface in F#: type IFoo<'T> = abstract member DoStuff : 'T -> unit If I implement this in C# I need the method signature to be: public void DoStuff<T>(T arg) {...} What I really want to do is reference FSharp.Core and then use: public Unit DoStuff<T>(T arg) {...} This would simplify other code because I wouldn't have to deal with Action vs Func. I'm guessing that there isn't any clean way to achieve this? How about some evil hacks? 回答1: Transformation of

Implement F# interface member with Unit return type in C#

拜拜、爱过 提交于 2019-12-01 19:00:58
Suppose I've defined the following interface in F#: type IFoo<'T> = abstract member DoStuff : 'T -> unit If I implement this in C# I need the method signature to be: public void DoStuff<T>(T arg) {...} What I really want to do is reference FSharp.Core and then use: public Unit DoStuff<T>(T arg) {...} This would simplify other code because I wouldn't have to deal with Action vs Func. I'm guessing that there isn't any clean way to achieve this? How about some evil hacks? Transformation of Unit to void is baked into the compiler. There's a FuncConvert class in F# Core for converting between

Void in constrast with Unit

久未见 提交于 2019-12-01 15:14:13
问题 I would like to understand which is the difference between these two programming concepts. The first represents the absence of data type and at the latter the type exists but there is no information. Additionally, I recognize that Unit comes from functional programming theoretical foundation but I still cannot understand what is the usability of the unit primitive (e.g., in an F# program). 回答1: The unit type just makes everything more regular. To an extent you can think of every function in F

F# - Function with no arguments?

偶尔善良 提交于 2019-12-01 02:04:13
When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value. However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it if it's not needed. I found a workaround, using let func _ = ... and calling it with func 1 or whatever, but that feels very non-idiomatic and confusing to the reader. This boils down to one question: In F#, Is there a proper way to declare a function with zero