我可以撰写纯函数:
let f x = x + 1
let g x = x + 2
let z = f . g
z 1 == 4
Run Code Online (Sandbox Code Playgroud)
我似乎也能够组成monadic函数:
let f x = Just (x + 1)
let g x = Just (x + 2)
let z x = f x >>= g
z 1 == Just 4
Run Code Online (Sandbox Code Playgroud)
我想我应该能够对待f并g从最后一个例子作为应用程序,并组成这些,但不确定如何:
let f x = Just (x + 1)
let g x = Just (x + 2)
let z x = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种更好的方法来检查给定类型的所有值是否相等.
例如,考虑:
data Foo = Foo {a :: Int, b :: Int, c :: Int, d :: Int}
allFooEqual :: Foo -> Bool
allFooEqual foo = (a foo == b foo) && (a foo == c foo) && (a foo == d foo)
Run Code Online (Sandbox Code Playgroud)
这可行,但它不是一个可扩展的解决方案.有没有更惯用的方式来执行我所缺少的这种行为?
test4 :: [Int] -> Bool
test4 [] = False
test4 (x:xs)
| mod x 3 /= 0 = False
| otherwise = True
Run Code Online (Sandbox Code Playgroud)
我只是喜欢代码找到一个不能被三分的列表中的任何(不仅仅是第一个)数字,然后说False.
我刚开始学习Haskell.
我正在尝试使类型与基本合并两个对象的通用数组reduce函数一起使用。以下代码段是实际代码的转储版本。为什么是fl类型{}而不是IFoo & IBar?
(我知道可以通过一个Object.assign()调用轻松替换这个特定示例。)
const flatten = <K, T>(prev: K, x: T): K & T => {
return Object.assign(prev, x);
};
interface IFoo {
foo: true;
}
interface IBar {
bar: true;
}
const fooRes: IFoo = { foo: true };
const barRes: IBar = { bar: true };
const fl = [fooRes, barRes].reduce(flatten, {});
console.log(fl); // here, fl : {}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 OSX 上的 .NET Core 中从 C# 调用 Roslyn。
public class Program
{
public static void Main()
{
var cscArgs = CSharpCommandLineParser.Default.Parse(
new[] { "/target:Library" },
Directory.GetCurrentDirectory(),
"/usr/local/share/dotnet/shared/Microsoft.NETCore.App/1.1.0/"
);
var refs = cscArgs.MetadataReferences.Select(x => MetadataReference.CreateFromFile(x.Reference, x.Properties));
foreach (var r in refs)
{
Console.WriteLine(r.FilePath);
}
var trees = new[]
{
CSharpSyntaxTree.ParseText("class Foo {}", cscArgs.ParseOptions)
};
var compilation = CSharpCompilation.Create(cscArgs.CompilationName, trees, refs, cscArgs.CompilationOptions);
using (var peStream = File.OpenWrite("test.dll"))
{
var emitResult = compilation.Emit(peStream, null, null, null, cscArgs.ManifestResources, cscArgs.EmitOptions);
foreach (var d in …Run Code Online (Sandbox Code Playgroud) let f = map tail.lines
f "fsdaf\nfdsf\n"
Run Code Online (Sandbox Code Playgroud)
它为什么有效?
let f = map tail.tail.lines
f "fasdf\nfasdfdsfd\n"
Run Code Online (Sandbox Code Playgroud)
我得到结果:
["asdfdsfd"]
let f = map (tail.tail).lines
f "fasdf\nfasdfdsfd\n"
Run Code Online (Sandbox Code Playgroud)
我得到结果:
["sdf","sdfdsfd"]
Run Code Online (Sandbox Code Playgroud)
我想知道haskell如何解析上面的代码.
我有以下数据类型(如列表):
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
Run Code Online (Sandbox Code Playgroud)
并创建了一个实例Monoid:
instance Monoid (List a) where
mempty = Nil
mappend Nil ys = ys
mappend (Cons x xs) ys = Cons x (mappend xs ys)
Run Code Online (Sandbox Code Playgroud)
我尝试编写一个实例来Arbitrary测试后者,但无法完成它:
instance Arbitrary a => Arbitrary (List a) where
arbitrary = do
Run Code Online (Sandbox Code Playgroud)
请帮助我完成该功能。
我的任务是实现这些功能,但我先陷入困境.
为什么我不能这样写empty?(ghci抱怨:couldn't match type)
我不是说那[]应该是type m k v吗?
class MapLike m where
empty :: m k v
empty = [] :: m k v
lookup :: Ord k => k -> m k v -> Maybe v
newtype ListMap k v = ListMap { getListMap :: [(k,v)] } deriving (Eq,Show)
Run Code Online (Sandbox Code Playgroud) 现在我知道,在使用 Haskell 声明函数签名后,我可以使用函数重载进行模式匹配,如下所示:
frog :: Int -> String
frog 1 = "Ribbit"
frog 7 = "Croak"
frog 12 = "Pop!"
frog x = replicate x "Z"
Run Code Online (Sandbox Code Playgroud)
我知道我也可以以类似的方式使用守卫:
frog :: Int -> String
frog x =
| x == 1 = "Ribbit"
| x == 7 = "Croak"
| x == 12 = "Pop!"
| otherwise = replicate x "Z"
Run Code Online (Sandbox Code Playgroud)
然而,我宁愿结合使用布尔防护和模式来确定将执行哪个臂的两种方式。类似于这个 Rust 片段的东西:
fn frog(x: u32) -> String {
match x {
k if k >= 1000 => todo!()
k if …Run Code Online (Sandbox Code Playgroud) 我试图过滤类型列表: IO [Either a b]
理想情况下,我想用以下类型的sig组成过滤函数:
(Monad m, Monad m2) => m [m2 a] -> (a -> Bool) -> m [m2 a]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多的组合filter,filterM,fmap和=<<妄图解除我的谓词到合适的范围内,但我一直缺少的标志-我可以做到m [m a]-> (a -> m Bool)-> m [m a],但由于这两个和IO是不一样的单子,这并未"好像对我好.
这似乎是'do notation'的用例,但是我一直无法找到一种方法来检查分配给<-操作员的事物的类型签名,因此我会在移动目标上进行拍摄.
我不确定如何以一种方式组合函数,以便明确它遍历包含不同monad(Either)实例的列表,然后是包含列表本身(IO)的monad.
我在这里错过了什么?