我正在学习do expression和Monad使用学习你很好的HASKELL.gcd使用tell函数的实现让我感到困惑.
gcd :: Int -> Int -> Writer [String] Int
gcd a b
| b == 0 = tell ["Finished with " ++ show a ] >>= (\_ -> return a)
| otherwise = tell [show a ++ " mod " ++ show b ++ " = " ++ show (a `mod` b)] >>= (\_ -> gcd b (a `mod` b))
gcdResult = gcd 8 3
-- result: WriterT …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 mac pro 上使用 i386-elf-gcc 学习操作系统。操作系统版本是 Sierra。我使用 Macports 通过键入sudo port install i386-elf-gccbash来安装编译环境。但是当我编译一个 helloworld 程序时,它出错了。
gcc hello.c
/opt/local/lib/gcc/i386-elf/4.7.2/../../../../i386-elf/bin/ld: cannot find crt0.o: No such file or directory
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
然后我使用sudo find / -name "crt0.o",但在我的 mac 中只有 crt1.o、crt3.o 等等。我使用 -nostdlib 和 -nostartfiles 它仍然出错:
gcc -nostdlib -nostartfiles hello.c
/opt/local/lib/gcc/i386-elf/4.7.2/../../../../i386-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000008048054
/var/folders/wc/0c_zn09x12s_y90ccmvjb6900000gn/T//cc384VWr.o: In function `main':
hello.c:(.text+0x11): undefined reference to `puts'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我的天啊。希望有人救救我。谢谢!!
我只是在学习STL,reverse_iterator让我很困惑.它有一个默认的构造函数,但我不明白如何使用它.我试过了:
reverse_iterator<int*> r{};
r --;
Run Code Online (Sandbox Code Playgroud)
程序崩溃了.我相信这种用法没有意义,它很容易导致崩溃,为什么允许使用默认构造函数?
我是 Haskell 新手,我想做一些有如下副作用的事情:
i = 3.0
main :: IO ()
main = let m = print i in putStrLn "Hello world"
Run Code Online (Sandbox Code Playgroud)
i然后我可以知道运行时的值main,但I没有打印。我!之前添加过m,但它也不起作用。我想知道如何破解这个,提前谢谢!
它似乎Box.clone()复制堆内存.据我所知,Box它会在超出其范围之后被破坏,以及它指向的内存区域.
所以我想问一种方法来创建Box指向同一内存区域的多个对象.
在 C 语言家族中,我可以在一行中做到这一点:
for(int i = lo, int j = mid+1; i <= mid && j <= hi; i++, j++){
...
}
Run Code Online (Sandbox Code Playgroud)
但是在 Rust 中……我只能这样写:
for i in lo..mid+1 {
let mut j = mid+1;
if j <= hi {
break;
}
...
j += 1;
}
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来实现这一点?
使用迭代器适用于上述情况,但使用迭代器会使某些场合使用算术变得麻烦,例如
for i in lo..mid+1 {
let mut j = mid+1;
if j <= hi {
break;
}
...
j += 1;
}
Run Code Online (Sandbox Code Playgroud)
在 Rust 中,这不起作用。变量i不会增加 5,而是增加 1:
for i …Run Code Online (Sandbox Code Playgroud) 使用相互引用的结构在单个文件中很常见,但是当我将结构分成两个文件时,我收到错误.
mod_1.rs
mod mod_2;
use mod_2::Haha;
pub struct Hehe {
obj: Haha,
}
fn main() {
Hehe(Haha);
}
Run Code Online (Sandbox Code Playgroud)
mod_2.rs
mod mod_1;
use mod_1::Hehe;
pub struct Haha {
obj: Hehe,
}
fn main() {
Haha(Hehe);
}
Run Code Online (Sandbox Code Playgroud)
这将产生错误.编译时mod_1.rs:
error: cannot declare a new module at this location
--> mod_2.rs:1:5
|
1 | mod mod_1;
| ^^^^^
|
note: maybe move this module `mod_2` to its own directory via `mod_2/mod.rs`
--> mod_2.rs:1:5
|
1 | mod mod_1;
| ^^^^^
note: ... …Run Code Online (Sandbox Code Playgroud) 我想在多态类型的参数和返回类型中定义一组函数,如下所示.
class Poly a b where
poly :: a -> b
instance Poly Int Int where
poly :: Int -> Int
poly a = a
Run Code Online (Sandbox Code Playgroud)
当我在ghci中测试它时,使用poly 3 :: Int然后它会给我错误:
*Poly> poly 3 :: Int
<interactive>:486:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘poly’
prevents the constraint ‘(Poly a0 Int)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instance exist:
instance Poly Int Int
-- Defined …Run Code Online (Sandbox Code Playgroud) sequenceA功能可以反转变量的容器非常棒- 例如,运行sequence (Just $ Right 3)会得到Right $ Just 3.我想展开sequenceA看它是如何工作的,但我被吸进去了...
-- source code of `sequenceA` for reference
traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
traverse f = sequenceA . fmap f
sequenceA :: Applicative f => t (f a) -> f (t a)
sequenceA = traverse id
-- unfold it
> sequenceA (Just $ Right 3)
> (traverse id) (Just $ Right 3)
> (sequenceA …Run Code Online (Sandbox Code Playgroud)