我认为它在我大学过去的论文中很简单,但功能是:
[(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?
我有一个包含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) 我想转换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)
这是什么原因?我们如何do
在where
声明中使用块?而且,我们是否有机会在警卫面前发表一些声明?
我正在尝试制作一个非常简单的类似蛇的游戏,如果您尝试使用已经访问过的斧头坐标,则会输掉该游戏。
到目前为止,这是起作用的代码(您可以使用箭头键移动播放器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) 我想将一个列表作为参数传递给一个函数,该函数将该列表的每个元素乘以3。我必须使用递归(我知道该怎么做)和map函数(有问题)。
我正在尝试将列表作为参数传递,就像我在其他帖子中看到的那样,但是它不起作用。
fun x = 3 * x + 1
mult :: [Int] -> [Int]
mult [a] = map fun [a]
Run Code Online (Sandbox Code Playgroud)
我尝试的代码显示:异常:x:函数mult中的非穷举模式
我打算编写一个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的新手,所以任何提示都很棒。
我正在尝试用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,但我遇到了困难,因为错误消息非常神秘。特别是,当我运行这个应该打印字符串排列数的代码时,我不理解错误消息,
import Data.List
main::IO()
main = do
str <- getLine
print putStrLn $ length $ nub $ permutations str
Run Code Online (Sandbox Code Playgroud)
我得到的错误信息是
但是,当我在 REPL 中运行它时,我没有收到这样的错误:
我正在尝试将函数映射到 Map (来自Data.Map)实例以将其转换为新类型的 Map。具体来说,我有两种类型的地图:
type Scope = Map.Map String AExpr
type Row = Map.Map String Value
Run Code Online (Sandbox Code Playgroud)
映射AExpr
到Value
(给定它的第一个参数 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) 我知道我可以使用守卫来检查列表中是否出现一个值,但我想知道是否也可以单独使用模式匹配。
-- 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 ×10
do-notation ×3
list ×3
io-monad ×2
map-function ×2
types ×2
arity ×1
casting ×1
dictionary ×1
if-statement ×1
monads ×1
ncurses ×1
scope ×1
tuples ×1