我一直在搞乱一些Haskell函数,有些我已经理解,有些则没有.
例如,如果我们这样做:scanl (+) 0 [1..3]我的理解如下:
1. the accumulator is 0 acc = 0 |
2. (+) applied to acc and first el acc = 0 + 1 = 1 |
3. (+) applied to latest acc and snd el acc = 1 + 2 = 3 |
4. (+) applied to latest acc and third acc = 3 + 3 = 6 V
Run Code Online (Sandbox Code Playgroud)
现在,当我们制作清单时,我们得到了[0, 1, 3, 6].
但我似乎无法理解如何scanr (+) 0 [1..3]给我:[6,5,3,0]
也许scanr …
我需要创建一个带有列表和元素的函数,并返回一个列表,其中第一次出现的元素被删除:类似于
removeFst [1,5,2,3,5,3,4,5,6] 5
[1,2,3,5,3,4,5,6]
Run Code Online (Sandbox Code Playgroud)
我尝试的是:
main :: IO()
main = do
putStr ( show $ removeFst [1,5,2,3,5,3,4,5,6] 5)
removeFst :: [Int] -> Int -> [Int]
removeFst [] m = []
removeFst [x] m
| x == m = []
| otherwise = [x]
removeFst (x:xs) m
| x == m = xs
| otherwise = removeFst xs m
Run Code Online (Sandbox Code Playgroud)
但这不起作用......它返回没有第一个元素的列表.我想我应该进行递归调用,使列表如下:
removeFst (x:xs) m
| x == m = xs
| otherwise = removeFst (-- return the whole list till element …Run Code Online (Sandbox Code Playgroud) 所以我有一个C++ DLL,我在我的c#应用程序中用于监视Windows消息.我想知道是否发送了WM_CLOSE和WM_QUERYENDSESSION,因为我无法从C#应用程序中看到它.如果我收到其中一条消息,我想用我的文件做一些清理,但问题是当我用TM杀死它时功能不起作用.它接缝我没有收到消息.我认为问题在于任务管理器向C#应用程序发送消息而不是向c ++ dll发送消息.
一些代码:
C++:
typedef void (*CLOSING_FUNCTION)();
CLOSING_FUNCTION myClosingFunction;
typedef void (*SHUTDOWN_FUNCTION)();
SHUTDOWN_FUNCTION myShutdownFunction;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
myClosingFunction();
return 0;
case WM_QUERYENDSESSION:
myShutdownFunction();
return 1;
case WM_DESTROY:
myClosingFunction();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
Run Code Online (Sandbox Code Playgroud)
C#:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Close_Function();
private static Close_Function myCloseDelegate;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void Shutdown_Function();
private static Shutdown_Function myShutdownDelegate;
static void StartMonotoring()
{
myCloseDelegate …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,我想知道如何在Haskell中打印两个函数的结果,我会在c ++中做的事情如下:
cout << f() << g();
Run Code Online (Sandbox Code Playgroud)
或者在c#中:
Console.WriteLine(f() + " " + g());
Run Code Online (Sandbox Code Playgroud)
在Haskell中,我尝试了类似的东西
main =
--putStr ( show $ square 3 )
putStr ( show $ fibSeries 12 )
square :: Int -> Int
square x = x * x
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
fibSeries :: Int -> [Int]
fibSeries x = map fib [0..x]
Run Code Online (Sandbox Code Playgroud)
但我不得不评论第一个命令,因为它给了我一个编译时错误.
错误是: …
有人可以向我解释为什么在将类转换为基类时调用重写方法:
class Base
{
public virtual void VirtualMethod()
{
Console.WriteLine("Base virtual method");
}
}
sealed class Derived : Base
{
public override void VirtualMethod()
{
Console.WriteLine("Overriden method");
}
}
static void Main(String[] args)
{
Derived d = new Derived();
((Base)d).VirtualMethod();
}
Run Code Online (Sandbox Code Playgroud)
我的意思是这段代码打印:
Overriden method
Run Code Online (Sandbox Code Playgroud)
并不是
Base virtual method
Run Code Online (Sandbox Code Playgroud)
它是一个运行时或编译时的未来?
我知道我可以通过调用派生来调用Base的虚方法,base.VirtualMethod()但是我可以从外面调用它吗?(比如来自Main或其他一些班级)
我正在尝试解决Project Euler的问题14(http://projecteuler.net/problem=14),并且我使用Haskell达到了死胡同.
现在,我知道这些数字可能足够小,我可以做一个蛮力,但这不是我锻炼的目的.我试图记住一个Map类型的中间结果,Map Integer (Bool, Integer)意思是:
- the first Integer (the key) holds the number
- the Tuple (Bool, Interger) holds either (True, Length) or (False, Number)
where Length = length of the chain
Number = the number before him
Run Code Online (Sandbox Code Playgroud)
例如:
for 13: the chain is 13 ? 40 ? 20 ? 10 ? 5 ? 16 ? 8 ? 4 ? 2 ? 1
My map should contain :
13 - (True, 10) …Run Code Online (Sandbox Code Playgroud)