小编max*_*loo的帖子

Haskell - 模式同义词在记录更新中的使用

我现在正在尝试一段代码:

https://mpickering.github.io/posts/2015-12-12-pattern-synonyms-8.html

{-# LANGUAGE PatternSynonyms #-}
pattern MyPoint :: Int -> Int -> (Int, Int)
pattern MyPoint{m, n} = (m,n)
m :: (Int, Int) -> Int
n :: (Int, Int) -> Int
test9 = (0,0) { m = 5 }
Run Code Online (Sandbox Code Playgroud)

但是test9抛出一个错误:

• ‘m’ is not a record selector
• In the expression: (0, 0) {m = 5}
Run Code Online (Sandbox Code Playgroud)

我如何test9上班?

haskell

5
推荐指数
1
解决办法
61
查看次数

C++ 我应该使用什么类型的指针来存储`algorithm` `remove` 函数的输出地址?

我试图将algorithm removeC++ 中函数返回的地址存储在一个变量中,但未能找到。我试过int*char*。两者都抛出了错误。

使用 Visual Studio CL,错误是: error C2440: '=': cannot convert from '_FwdIt' to 'int *'

使用MinGW,错误是: cannot convert '__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >' to 'int*' in assignment

我应该如何存储这样的地址?

我正在尝试的代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main (void) {
  string line ("This is an example sentence.");
  int* newEOL;
  newEOL = remove(line.begin(), line.end(), ' ');
  printf("%p\n", newEOL);
}
Run Code Online (Sandbox Code Playgroud)

c++ variables pointers

2
推荐指数
1
解决办法
43
查看次数

haskell *&gt; 排序运算符

我在回答我的问题时遇到了一个问题: haskell 要么和 Validation Applicative

我的代码贴在那里。

它涉及使用*>排序运算符而不是<*>应用运算符。

根据https://hackage.haskell.org/package/base-4.15.0.0/docs/Control-Applicative.html#v:-42--62-上的解释,我理解对*>动作进行排序,丢弃的值第一个论点。所以对于我的代码,我已经尝试过fail6 = fail2 *> success,它可以工作,但它不应该工作,因为第一个参数的值,即fail2,应该被丢弃。为什么fail6有效?

的输出fail6Failure [MooglesChewedWires,StackOverflow]

haskell sequencing

2
推荐指数
1
解决办法
81
查看次数

通过等式推理了解 Monad CountMe 的绑定

这个问题来自于“Haskell Programming from first Principles”一书的第 18.5 节“Monad 法则”的“Bad monads and their denizens”一节。

data CountMe a =
  CountMe Integer a
  deriving (Eq, Show)
instance Functor CountMe where
  fmap f (CountMe i a) =
    CountMe i (f a)
instance Applicative CountMe where
  pure = CountMe 0
  CountMe n f <*> CountMe n' a =
    CountMe (n + n') (f a)
instance Monad CountMe where
  return = pure
  CountMe n a >>= f =
    let CountMe n' b = f a
    in CountMe …
Run Code Online (Sandbox Code Playgroud)

monads haskell

1
推荐指数
2
解决办法
112
查看次数

Haskell - 使用 foldr 为函数 `all` 创建函数定义

我正在尝试all使用foldr. p是谓词。我知道这是可以做到的:

all p = and . foldr (\x xs -> p x : xs) []
Run Code Online (Sandbox Code Playgroud)

但我想做的是将函数and转换为foldr方程。这能做到吗?

我尝试了以下方法,但都失败了:

all p = foldr (\x p -> \ys -> and (p x) ys) True
all p = foldr (\x and -> (\ys -> (p x and ys))) True
all p = foldr (\x ys -> and . (p x) ys) True
Run Code Online (Sandbox Code Playgroud)

我对如何申请的理解不足foldr吗?

haskell predicate fold

-2
推荐指数
1
解决办法
108
查看次数

标签 统计

haskell ×4

c++ ×1

fold ×1

monads ×1

pointers ×1

predicate ×1

sequencing ×1

variables ×1