我想有一个函数检查列表是否只包含偶数; 如果是这样,它应该返回True
,否则 - False
.
功能我想用的map
/ filter
/ foldr
,可能没有length
.
这是我的尝试:
ListOfeven :: [Integral] -> Bool
ListOfeven xs =
| foldr (+) True filter odd xs < 0 = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
我很确定有一种更清洁的方式......不存在吗?:)
我在使用show
打印由列表列表给出的矩阵行时遇到了一些麻烦.
我有这个:
data Matrix = Mat Int [[Bit]]
deriving Eq
Run Code Online (Sandbox Code Playgroud)
其中参数Int
是平方矩阵的阶数,并且Bit
是Int(0或1).我需要我的代码能够执行以下操作,Matrix
作为以下实例Show
:
Main> Mat 3 [[0,0,1],[1,0,1],[1,1,1]
[0,0,1]
[1,0,1]
[0,0,1]
Run Code Online (Sandbox Code Playgroud)
到目前为止我只有:
instance Show Matrix where
show (Mat i (x:xs)) = (show x) ++ "\n"
Run Code Online (Sandbox Code Playgroud)
但这显然只返回第一个列表.你能帮我解决这个问题吗?提前致谢.
我试图匹配字典中的单词,不区分大小写.我最初的方法是这样的:
是否有更好(更有效)的方法来实现这一目标?我是Haskell的新手.
import System.IO
import Data.Text (toLower, pack, unpack)
import Data.Set (fromList, member)
main = do
let path = "/usr/share/dict/american-english"
h <- openFile path ReadMode
hSetEncoding h utf8
contents <- hGetContents h
let mySet = (fromList . map (unpack . toLower . pack) . lines) contents
putStrLn $ show $ member "acadia" mySet
Run Code Online (Sandbox Code Playgroud) 我正在努力学习Haskell,这是我在函数式编程中的第一种方法.我在创建一个以数字作为输入并逐个打印该数字的函数时遇到了一些麻烦,直到0.
printDescending n = if n > 0
then printDescending n - 1 return n
else return n - 1
Run Code Online (Sandbox Code Playgroud)
我希望能够做到printDescending 20
并输出20,19,18 ...... 2,1,0.但是我收到了这个错误:
> • Non type-variable argument
> in the constraint: Num ((a -> m a) -> a1 -> m1 a1)
> (Use FlexibleContexts to permit this)
> • When checking the inferred type
> printDescending :: forall (m :: * -> *) a (m1 :: * -> *) a1.
> (Ord a1, Num ((a -> …
Run Code Online (Sandbox Code Playgroud) 在这里的代码中
trait Foo {
type Output;
fn foo(self) -> Self::Output;
}
impl<'a> Foo for &'a () {
type Output = &'a ();
fn foo(self) -> Self::Output {
self
}
}
fn func<F: Foo>(f: F) -> F::Output {
f.foo()
}
fn func2<'a>(f: &'a ()) -> &'a () {
func::<&'a ()>(f)
}
fn has_hrl<F: Fn(&()) -> &()>(f: F) {}
fn main() {
//has_hrl(func); // FAILS
has_hrl(func2);
has_hrl(|x| func(x));
}
Run Code Online (Sandbox Code Playgroud)
我们想做has_hrl(func)
,但Rust只接受关闭has_hrl(|x| func(x))
.这是为什么?因为它适用于类似的具体类型func2
,但不适用于泛型类型.
我正在看的代码包含这个顶级函数:
starman :: String -> Int -> IO ()
starman word n = turn word ['-' | x <- word] n
Run Code Online (Sandbox Code Playgroud)
我可以看到这是一个带有2个参数的函数,然后调用如下所示的turn函数:
turn :: String -> String -> Int -> IO ()
turn word display n =
do if n==0
then putStrLn "You lose"
else if word==display
then putStrLn "You win!"
else mkguess word display n
Run Code Online (Sandbox Code Playgroud)
我不明白的是这意味着什么:
word ['-' | x <- word]
Run Code Online (Sandbox Code Playgroud) 我现在已经在这里停留了一段时间,无法弄清楚如何让这个领域变得word
可变.有人可以在这里指出我的问题.
pub struct Person<'name>{
name:&'name Name
}
pub struct Name{
word: String
}
impl<'name> Person<'name>{
pub fn new(name:&'name mut Name)-> Person<'name>{
Person {
name: name
}
}
}
fn main(){
let mut name: Name = Name {
word: String::from("Petre")
};
let mut person: Person = Person::new(&mut name);
first(&mut person);
}
pub fn first(person:&mut Person){
person.name.word = String::from("Wurst");
second(person);
}
pub fn second(person:&mut Person){
println!("{}",person.name.word)
}
Run Code Online (Sandbox Code Playgroud)
产量
Run Code Online (Sandbox Code Playgroud)error: cannot assign to immutable field `person.name.word` --> main.rs:27:5 | 27 | …
我lazy_static
用来留HashMap
在记忆中.有两种方法,我添加和获取元素,但我有一些生命周期的问题.
这是我的代码:
#[macro_use]
extern crate lazy_static;
use std::sync::Mutex;
use std::collections::HashMap;
lazy_static! {
static ref HASHMAP: Mutex<HashMap<String, Foo>> = Mutex::new({
let mut m = HashMap::new();
m.insert("one".to_string(), Foo{param1:"foo1".to_string(), param2:"foo2".to_string()} );
m.insert("two".to_string(), Foo{param1:"bar1".to_string(), param2:"bar2".to_string()});
m
});
}
pub struct Foo{
param1: String,
param2: String,
}
pub fn ins_val(name: String, f: Foo){
HASHMAP.lock().unwrap().insert(name, f);
}
pub fn get_val(k: &str) -> Option<&Foo>{
HASHMAP.lock().unwrap().get(k)
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
HASHMAP.lock().unwrap().get(k)
^^^^^^^^^^^^^^^^^^^^^^^
reference must be valid for the anonymous lifetime #1 defined on the block
Run Code Online (Sandbox Code Playgroud) 我正在学习我在Rust上发现的一些教程,并且遇到了我的Java/C/C++头脑无法理解的东西:
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})\n({}, {})", self.0, self.1, self.2, self.3)
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白write!
宏调用结束时缺少分号.如果我添加它,我会从编译器中收到错误.我猜测如果分号不存在则将Result
from write!
用作返回值fmt
,但任何人都可以提供更具体的解释,为什么会这样,如果它总是适用?
我是Rust的新手.我知道Rust会在编译时预测绑定的类型.下面的代码编译并运行.
fn main() {
let mut numbers = Vec::new();
numbers.push(1);
}
Run Code Online (Sandbox Code Playgroud)
numbers
矢量的默认类型是什么?