小编Mor*_*ang的帖子

〜before(l,r)是什么意思

我读了Haskell的图书馆

partitionEithers :: [Either a b] -> ([a],[b])
partitionEithers = foldr (either left right) ([],[])
 where
  left  a ~(l, r) = (a:l, r)
  right a ~(l, r) = (l, a:r)

Run Code Online (Sandbox Code Playgroud)

~before 是什么意思(l, r)

haskell pattern-matching lazy-evaluation

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

为什么在这个不相交的联合中这里1和2

我研究了Haskell中Either的用法,

这是我们课程中的一个例子,但是我不明白为什么1和2而不是每个集合中的每个元素。

问题的形象

谢谢

haskell

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

如何在此处解决“ ***例外:Prelude.head:空列表”

这是一项作业,它是删除相邻的重复项。结果应该像这样removeAdjacentDuplicates [3,1,2,2,2,2,2,4,4,2,2,3] == [3,1,2,4,2,3]

我知道没有必要在head这里使用,但是不允许使用rekursion和格式为[e | ...]。仅Prelude中的功能是允许的,group依此类推,在其他软件包中也不允许。map zip filter concat reverse foldr推荐。

例如,不可能做到这一点:

removeAdjacentDuplicates :: Eq a => [a] -> [a]
removeAdjacentDuplicates (x:xs@(y:_))
 | x == y    = x:tail (removeAdjacentDuplicates xs)
 | otherwise = x:removeAdjacentDuplicates xs
Run Code Online (Sandbox Code Playgroud)

所以我尝试这样

removeAdjacentDuplicates = foldr (\x result -> if ( x == (head result)) then result else (x : result)) []

Run Code Online (Sandbox Code Playgroud)

但是当我测试它时,它就扔掉*** Exception: Prelude.head: empty list' here

我曾经尝试添加removeAdjacentDuplicates [] = []
但是错误是这样的

Equations for …
Run Code Online (Sandbox Code Playgroud)

haskell

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

为什么“变量不在范围内”

代码有什么问题。

我的Haskell平台是新的。而且我已经尝试过多次调整格式。但这并非一直有效。

import Data.Char
import Data.List

encode :: Int -> String -> String
encode shift msg =
    let ords = map ord msg
    shifted = map (+ shift) ords
    in map chr shifted
Run Code Online (Sandbox Code Playgroud)

结果总是这样

Prelude> :r
Ok, no modules loaded.
Prelude> :type encode

<interactive>:1:1: error: Variable not in scope: encode
Run Code Online (Sandbox Code Playgroud)

当我加载文件时,它显示

Prelude> :l H2-2.hs
[1 of 1] Compiling Main             ( H2-2.hs, interpreted )

H2-2.hs:56:3: error: parse error on input ‘shifted’
   |
56 |   shifted = map (+ shift) ords
   | …
Run Code Online (Sandbox Code Playgroud)

haskell

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

为什么在'do {let {the = Just“ Hi!”}中将'Nothing'作为输出?_ &lt;-序列[the,Nothing]; 纯粹是“没办法!” }'

就像标题一样,代码如下:

do{ let{ the= Just "Hi!"}; _ <- sequence [the,Nothing]; pure "no way!" }

输出为Nothing。我的问题是如何确定这个Do-block是Maybe-Monad?do作为语法糖的确切功能是什么?

monads haskell

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