小编Wil*_*ess的帖子

列表理解输出不是我假设的

我认为它在我大学过去的论文中很简单,但功能是:

[(x,y) | x <- [0..2], y<-[0,x])
Run Code Online (Sandbox Code Playgroud)

和输出

  [(0,0),(0,0),(1,0), (1,1), (2,0), (2,2)]
Run Code Online (Sandbox Code Playgroud)

(2,0)混淆了我,如果y映射到0到x而x等于1 =(1,1)则不会

[(0,0),(0,0),(1,0), (1,1), **(2,1)**, (2,2)]
Run Code Online (Sandbox Code Playgroud)

或者是因为由于y使用其列表[0,1]中的所有数字,它还原为0?

haskell

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

Haskell - 索引列表

我有一个包含3个元组项目的列表,我想根据第一个项目索引列表,我已经编写了一个对我来说听起来合理的代码但是我遇到了类型错误,这就是我写的内容

addIndex [] indexed = indexed
addIndex ((a1,b1,c1):xs) [] 
                    = addIndex xs [(a1,b1,c1,0)]
addIndex ((a1,b1,c1):xs) indexedWIP 
                    = addIndexH ((a1,b1,c1):xs) indexedWIP (last indexedWIP)
addIndexH ((a1,b1,c1):xs) indexedWIP (ax,bx,cx,ix)
                    = if (a1 /= ax) 
                        then (addIndex xs (indexedWIP ++ (a1,b1,c1,(ix+1)))) 
                        else (addIndex xs (indexedWIP ++ (a1,b1,c1,(ix))))
Run Code Online (Sandbox Code Playgroud)

我收到以下类型错误

ERROR file:.\lmaogetrektson.hs:109 - Type error in application
*** Expression     : indexedWIP ++ (a1,b1,c1,ix + 1)
*** Term           : (a1,b1,c1,ix + 1)
*** Type           : (b,c,d,e)
*** Does not match : [a]
Run Code Online (Sandbox Code Playgroud)

haskell types tuples list type-mismatch

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

在where子句下做声明

我想转换IO [String][String]具有<-约束力.但是,我需要do在一个where声明中使用一个块来做到这一点,但是Haskell一直抱怨缩进.这是代码:

decompEventBlocks :: IO [String] -> IO [[String]]
decompEventBlocks words
 | words' /= [] = block : (decompEventBlocks . drop $ (length block) words')
 | otherwise = []
  where 
   do
    words' <- words
    let block = (takeWhile (/="END") words')
Run Code Online (Sandbox Code Playgroud)

这是什么原因?我们如何dowhere声明中使用块?而且,我们是否有机会在警卫面前发表一些声明?

monads haskell scope functional-programming do-notation

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

do块中的if语句给出错误消息

我正在尝试制作一个非常简单的类似蛇的游戏,如果您尝试使用已经访问过的斧头坐标,则会输掉该游戏。

到目前为止,这是起作用的代码(您可以使用箭头键移动播放器1并使用wasd移动播放器2):

import UI.NCurses

main :: IO ()    
main = runCurses $ do
    w <- defaultWindow
    updateWindow w $ do
        drawBorder Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
    render
    loop w [] 1 1 0 0 10 10 0 0

loop :: Window -> [(Integer, Integer)] -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer ->  Curses ()
loop w list p1x p1y p1oldDx p1oldDy p2x p2y p2oldDx p2oldDy = do
    e …
Run Code Online (Sandbox Code Playgroud)

haskell if-statement ncurses do-notation io-monad

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

如何修复“功能中的非穷举模式”

我想将一个列表作为参数传递给一个函数,该函数将该列表的每个元素乘以3。我必须使用递归(我知道该怎么做)和map函数(有问题)。

我正在尝试将列表作为参数传递,就像我在其他帖子中看到的那样,但是它不起作用。

fun x = 3 * x + 1
mult :: [Int] -> [Int]
mult [a] = map fun [a]
Run Code Online (Sandbox Code Playgroud)

我尝试的代码显示:异常:x:函数mult中的非穷举模式

haskell pattern-matching non-exhaustive-patterns

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

如何在Haskell中使用带有变量和列表的map

我打算编写一个map函数,该函数本质上需要一个变量和一个列表,并返回一个列表。

我尝试使用标准地图,但是从这里我看到的是它在格式“地图功能列表”中时,我试图传递另一个参数,这是另一点。

data Point = {xCoord :: Int,
              yCoord :: Int}

movePoint :: Point -> Point -> Point
movePoint (Point x y) (Point xMove yMove)
    = Point (x + xMove)  (y + yMove)

 // Add a "vector" to a list of points
movePoints :: [Point] -> Point -> [Point]
movePoints = error "Not yet"
Run Code Online (Sandbox Code Playgroud)

例如,如果我有一个矢量,例如(2,2),并且我有一个点列表,例如[(-2,1),(0,0),(5,4)等],我想使用映射以将(2,2)添加到列表中的所有点并返回点列表,我不确定该怎么做。我是Haskell的新手,所以任何提示都很棒。

haskell functional-programming list arity map-function

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

Haskell中的“无法将类型'[]'与'IO'匹配”错误

我正在尝试用Points(我创建的数据类型)列出一个列表,这个想法是在每次迭代中添加一个元素。出了点问题。

我已经尝试过p了,myLoop但它似乎也不起作用。

main = myLoop 
myLoop  = do 
            let p = []
            done <- isEOF
            if done
              then putStrLn ""
              else do inp <- getLine
                      let (label:coord) = words inp
                      p ++  [Point label (map getFloat coord)]
                      -- print (pointerList)
                      myLoop 
Run Code Online (Sandbox Code Playgroud)

我得到这个输出

trabalho.hs:30:23: error:
    • Couldn't match type ‘[]’ with ‘IO’
      Expected type: IO Point
        Actual type: [Point]
    • In a stmt of a 'do' block:
        p ++ [Point label (map getFloat coord)]
      In the expression: …
Run Code Online (Sandbox Code Playgroud)

haskell list type-mismatch do-notation io-monad

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

Haskell 中排列的打印长度问题

我刚开始学习 Haskell,但我遇到了困难,因为错误消息非常神秘。特别是,当我运行这个应该打印字符串排列数的代码时,我不理解错误消息,

import Data.List

main::IO()
main = do
        str <- getLine
        print putStrLn $ length $ nub $ permutations str
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息是

在此处输入图片说明

但是,当我在 REPL 中运行它时,我没有收到这样的错误:

在此处输入图片说明

haskell

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

映射(fromList)上的 Haskell 映射函数到新类型

我正在尝试将函数映射到 Map (来自Data.Map)实例以将其转换为新类型的 Map。具体来说,我有两种类型的地图:

type Scope = Map.Map String AExpr
type Row = Map.Map String Value
Run Code Online (Sandbox Code Playgroud)

映射AExprValue(给定它的第一个参数 Scope )的函数:

evalAExpr :: Scope -> AExpr -> Value
Run Code Online (Sandbox Code Playgroud)

还有一个 type 的实例Scope,比如说x,我想为它映射函数evalAExpr以获得一个 type 的实例Row

根据文档,这应该可以简单地使用:

map :: (a -> b) -> Map k a -> Map k b
Run Code Online (Sandbox Code Playgroud)

所以在我的情况下,这将是:

x :: Scope
evalAExpr :: Scope -> AExpr -> Value
y = map (evalAExpr x) x :: Row …
Run Code Online (Sandbox Code Playgroud)

dictionary haskell types casting map-function

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

模式匹配可以用于列表成员资格吗?

我知道我可以使用守卫来检查列表中是否出现一个值,但我想知道是否也可以单独使用模式匹配

-- Using guards
f :: [Int] -> Int
f xs
    | 42 `elem` xs = 42
    | otherwise = 0

-- Using pattern matching?
g :: [Int] -> Int
g (_:)*42:_ = 42  -- i.e. zero or more elements: we discard until 42, followed by whatever.
g _         = 0
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching

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