在此C#代码段中,DateTime.Now.Month.ToString()返回7输出.
我想得到07一个返回值.
当月份只有1位数时,我该怎么做才能添加前导零?
目前使用GHC,Data.Data/Data.Typeable和GHC.Generics进行泛型编程的选项有2个(如果你计算的是3个),那么这两个选项都可以从基础包中获得.那么,每个的优点和缺点是什么?GHC.Generics是"现代"方式而Data.Data是过时的,只是为了向后兼容而保留?
我已经定义了以下函数来反转Int或string的列表:
myReverse :: [a] -> [a]
myReverse [] = []
myReverse (x:xs) = (myReverse xs) ++ [x]
Run Code Online (Sandbox Code Playgroud)
我用hspec测试:
describe "myReverse" $ do
it "returns the inversed list of the given list" $ do
myReverse [1,2,3,4] `shouldBe` [4,3,2,1]
it "returns the inversed string of the given string" $ do
myReverse "A man, a plan, a canal, panama!" `shouldBe` "!amanap ,lanac a ,nalp a ,nam A"
Run Code Online (Sandbox Code Playgroud)
像这样我得到了警告
tests/OneToTenSpec.hs:69:24:
Warning: Defaulting the following constraint(s) to type `Integer'
(Eq a0)
arising from a use …Run Code Online (Sandbox Code Playgroud) 在GHCi中:(>表示输出)
data Unit = Unit
let x = Unit
let y = ()
:p x
> x = (_t1::Unit)
:p y
> y = ()
:i ()
> data () = () -- Defined in `GHC.Tuple'
Run Code Online (Sandbox Code Playgroud)
为什么Unit和()表现不一样?还有其他类型的行为(),例如Int和Char.还有其他类型的吗?
出人意料的是,当我更换()的undefined,它的行为,我再次希望:
let y = undefined :: ()
:p y
y = (_t2::())
Run Code Online (Sandbox Code Playgroud) 我想CreatePipe通过FFI 使用WinAPI 在Windows上创建一个匿名管道.这将给我一个HANDLE(来自Win32haskell包的类型),但我想获得一个普通的haskell,Handle这样我就可以使用标准的haskell IO函数了.所以我需要一个类型的函数:
win32handleToStandardHandle :: HANDLE -> IO Handle
Run Code Online (Sandbox Code Playgroud)
我该如何实现呢?
在Linux上,我可以使用System.Posix.IO的fdToHandle功能的之间的转换FD由Linux系统调用和标准Haskell的类型使用类型.但似乎没有Windows的这种功能.
编译以下代码:
import Prelude hiding (nonExistent)
main = return ()
Run Code Online (Sandbox Code Playgroud)
随着ghc -Wall给出:
test.hs:1:1: Warning:
Module `Prelude' does not export `nonExistent'
Run Code Online (Sandbox Code Playgroud)
是否有-fno-XXX禁用此特定警告的标志?
这是演示我的问题的代码:
trait T {
type A;
fn get(&mut self) -> Self::A;
}
struct Foo;
impl T for Foo {
type A = i32;
fn get(&mut self) -> i32 { 3 }
}
struct Wrapped<F, V> {
func: F,
cached: Option<V>,
}
impl<F, R> T for Wrapped<F, R::A>
where F: FnMut() -> R,
R: T,
R::A: Copy,
{
type A = R::A;
fn get(&mut self) -> R::A {
let v: R::A = self.cached.unwrap_or_else(|| (self.func)().get());
self.cached = Some(v);
v
}
}
fn …Run Code Online (Sandbox Code Playgroud) 给出以下代码:
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Test where
data X = X !Int !Int
test (X a b) (X c d) = X (max a c) (max b d)
Run Code Online (Sandbox Code Playgroud)
GHC在使用优化进行编译时生成此核心(重命名以使读取更容易):
test
test =
\ u v ->
case u of x { X y z ->
case v of c { X d e ->
case tagToEnum# (<=# y d) of _ {
False ->
case tagToEnum# (<=# z e) of _ {
False -> x;
True -> X y …Run Code Online (Sandbox Code Playgroud) 当我在haskell中有如下数据类型时:
data A ctx = A (forall a. ctx a => a -> a)
Run Code Online (Sandbox Code Playgroud)
然后我可以将处理给定类的类型值的函数放入此数据类型中:
> A (+3) :: A Num
A (+3) :: A Num :: A Num
Run Code Online (Sandbox Code Playgroud)
但是也可以将具有多个约束的函数放入此数据类型中吗?我试过这个:
> :t ((+ 3) . succ)
((+ 3) . succ) :: (Enum c, Num c) => c -> c
> :t A ((+ 3) . succ) :: A (Enum,Num)
Expecting one more argument to `Enum'
In an expression type signature: A (Enum, Num)
In the expression: A ((+ 3) . …Run Code Online (Sandbox Code Playgroud)