小编eps*_*lbe的帖子

vim在一行中的块选择内替换

我在latex文件中有以下表达式

\begin{dfn} \tag{Diagram $$ over a trisp $\Delta$}
          \label{dfn:Diagram D over a trisp Delta}
Run Code Online (Sandbox Code Playgroud)

现在我想替换第二行括号中的所有空格:.我的想法是通过v%在其中一个括号上进行可视化选择表达式,然后:s/\ /:/g将其替换为所有空格并导致

::::::::::::::\label{dfn:Diagram::over:a:trisp:Delta}
Run Code Online (Sandbox Code Playgroud)

这导致删除:之前的任务\label{…} vim是一个非常强大的编辑器 - 应该有一个更简单的方法,我还不知道.

vim substitution

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

如何在Haskell中正确处理IO返回值

嗨,我有一个非常noob的问题,让我说我想创建一个游戏,当你必须回答问题时,我写了这个

data Question = Question { answer::String, text::String }
data Player = Player { name::String, points::String }

answerQuestion ::  Question -> Player -> Player 
answerQuestion question player
    | isCorrect question playerAnswer = Player (name player) (points player + 1)
    | otherwise = player
    where
      playerAnswer = do
          putStrLn text(question)
          getLine

isCorrect ::  Question -> String -> Bool 
isCorrect question try = try == answer(question)
Run Code Online (Sandbox Code Playgroud)

现在playerAnswer有类型IO String所以我必须isCorrectdo块内调用吗?有另一种方式来解析IO StringString

在第一种情况下,我感觉失去了函数式编程的所有好处,因为我最终会在do块中编写我的整个代码以便访问 …

io haskell functional-programming

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

尝试在Constructor值上执行haskell模式匹配

我有一个函数getImage,它接受DynamicImage类型的输入并将其更改为图像.功能如下

getImage (ImageY8 image) = image 
getImage (ImageY16 image) = image
Run Code Online (Sandbox Code Playgroud)

以上定义来自Codec.Picture模块.但它给了我一个错误:

Couldn't match type ‘GHC.Word.Word16’ with ‘GHC.Word.Word8’
    Expected type: Image Pixel8
      Actual type: Image Pixel16
    In the expression: image
    In an equation for ‘getImage’: getImage (ImageY16 image) = image
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用,因为我可以执行以下操作:

data Shape = Circle Float | Rectangle Float Float

area (Circle r) = 3.14 * r * r
area (Rectangle a b) = a * b
Run Code Online (Sandbox Code Playgroud)

这与我的问题类似.

haskell pattern-matching

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

Haskell:将索引列表映射到字符串

您好:我有一个句子分解为单独的单词:

["this", "is", "a", "simple", "sentence"]
Run Code Online (Sandbox Code Playgroud)

我有一份清单 indices = [0, 2, 4]

我试图将索引列表映射到句子上以返回适当索引处的单词,如下所示:如果我们应用于indices [0, 2, 4]句子,我们得到:

["this", "a", "sentence"]
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

sentence !! [x | x <- indices]
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

  <interactive>:215:7: error:
• Couldn't match expected type ‘Int’ with actual type ‘[Integer]’
• In the second argument of ‘(!!)’, namely ‘[x | x <- indices]’
  In the expression: tex !! [x | x <- indices]
  In an equation for ‘it’: it = sentence !! [x | x <- indices
Run Code Online (Sandbox Code Playgroud)

我对使用 …

indexing dictionary haskell

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

haskell -skipping getLine

嘿 - 伟大的程序员和haskellers,我是一名哈斯克尔大学新生并且有一个程序问题归结为以下情况

main :: IO ()
main = do
    putStrLn "\nplease give me some input"
    input1 <- getLine
    putStrLn "\nplease give me another input"
    input2 <-getLine
    putStrLn ("\nyour inputs were "++show(input1)++" and "++ show(input2)")
    putStrLn "restart ?? yY or nN"
    c <- getChar
    restart c
    where 
    restart c
        |elem c "yY" = do
            main
        |elem c "nN" = putStrLn "\nExample Over"
        |otherwise = do
            putStrLn "\nyou must type one of Yy to confirm or nN to abort"
            c'<- getChar …
Run Code Online (Sandbox Code Playgroud)

haskell getline

0
推荐指数
1
解决办法
519
查看次数

为什么不给出正确的输出?

count :: Eq a => a -> [a] -> Int
count _[]                  = 0
count z (x:xs) | z == x    = 1 + (count z xs)
               | otherwise = count z xs

rmdups :: Eq a => [a] -> [a]
rmdups [] = []
rmdups (x:xs) | count x xs > 0 = rmdups xs
              | otherwise     = x: rmdups xs
Run Code Online (Sandbox Code Playgroud)

上面的代码编译,但是当我输入'ababca'rmdups的输出时'bca',我希望它是'abc'.为什么去那里?我一直在改变代码,并且不知道为什么会这样做.

haskell

0
推荐指数
1
解决办法
247
查看次数

Haskell中的构造函数

这是家庭作业的摘录."Prog此数据类型应该只有一个构造函数,并用于表示窗体的程序:

read vin1 ; read vin2 ; < some statements > write vout2 ;
Run Code Online (Sandbox Code Playgroud)

这个构造函数也可以命名为Prog,它接受一个字符串作为两个输入和一个输出变量的名称."

将expProg定义为Prog类型,并将其作为上面左侧程序的抽象语法表示.这应该做如下:

expProg = Prog "x" "y" <some statements> "z"
Run Code Online (Sandbox Code Playgroud)

我是Haskell的新手,对此感到困惑.

我做了这样的构造函数.然而,这似乎不对.

data Prog = Prog String String String 
  deriving (Show,Eq)
Run Code Online (Sandbox Code Playgroud)

谁能解释一下这里发生了什么?我不明白如何制作这个构造函数.这是我为语句做的数据类型:

 data Stmt = Assing String Expr
      | WhileLoop Expr Stmt
      | Ifthen Expr Stmt
      | IfthenElse Expr Stmt Stmt
      | Composition [Stmt]
Run Code Online (Sandbox Code Playgroud)

haskell abstract-syntax-tree

0
推荐指数
1
解决办法
110
查看次数

Char to int.哈斯克尔

我想知道下面描述的功能是否可用Prelude.是吗?(我知道ord,但它在Data.Char)

我在寻找:

f :: Char -> Int
f 'a' = 0
f 'b' = 1
....
Run Code Online (Sandbox Code Playgroud)

haskell

0
推荐指数
1
解决办法
342
查看次数

Haskell"类型变量`a0'是模糊的"错误

我正在尝试为这个系列写一个函数.

Cos系列

代码在这里:

fact x = product [1..x]

cosSeries x l = sum[(helperOfCos x i pointer)
        |i<-[0..l], i `mod` 2 == 0, pointer<-[0..l]]


helperOfCos x i p = if p `mod` 2 == 0
        then x**i/(fact i)
        else -(x**i)/(fact i)
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

*Main> cosSeries 2 2

<interactive>:2:1:
No instance for (Integral a0) arising from a use of `cosSeries'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
  instance …
Run Code Online (Sandbox Code Playgroud)

haskell functional-programming

0
推荐指数
1
解决办法
428
查看次数

将列表拆分为多个部分,然后将这些部分混洗:Haskell

问题是:定义一个shuffle :: Int -> [a] -> [a]采用自然数n和偶数列表的函数,然后拆分然后将列表重复n次.例如,shuffle 2 [1,2,3,4,5,6] = [1,5,4,3,2,6].我有一个相应的函数riffle,但我不知道如何拆分列表.

我的浅滩功能是:

riffle :: [a] -> [a] -> [a]
riffle [] ys = ys
riffle xs [] = xs
riffle (x:xs)(y:ys) = x : y : riffle xs ys 
Run Code Online (Sandbox Code Playgroud)

我开始洗牌,我想,这就是我所拥有的:

shuffle :: Int -> [a] -> [a]
shuffle [] = []
shuffle a xs = (length xs) 'div' a
Run Code Online (Sandbox Code Playgroud)

我试图列出一个列表并分成指定为"a"的部分.我是Haskell的新手,我仍然不确定它是如何工作的:所有的帮助都表示赞赏.

recursion haskell list

0
推荐指数
1
解决办法
229
查看次数

如何在Haskell中重新实现"all"功能?

我需要定义一个函数all' :: ( a -> Bool ) -> [a] -> Bool来验证列表中的所有元素是否满足某个条件.

例如:all' ( <5) [1,2,3] = True, all' (>=2) [1,1,2,2,3,3]= False.

我的主要问题是我不知道如何处理函数的传输.

haskell haskell-platform

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

如何将元素乘法应用于haskell中的两个CArrays?

就像我得到了a CArray [1,2,3]和a一样CArray [6,7,8],我想逐个元素地乘以得到[6, 14, 24].你能给我一个演示吗?谢谢.

arrays haskell

-3
推荐指数
1
解决办法
122
查看次数