我试图在haskell中复制UNIX程序wc.为了使这更容易,我创建了一个类型:
data WCResult = WCResult {
wordCount :: Int,
fileName :: String
} --deriving (Show)
instance Show (WCResult x y) where
show (WCResult x y) = show x ++ " " ++ y
Run Code Online (Sandbox Code Playgroud)
当我尝试运行这个程序时,我得到了
wc.hs:9:15:
`WCResult' is applied to too many type arguments
In the instance declaration for `Show (WCResult x y)'
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么?
我一直试图在haskell 做第二个项目欧拉问题,但我一直在:Occurs check: cannot construct the infinite type: a = [a]
fibonacci 0 _ = 0
fibonacci 1 _ = 1
fibonacci x xs = (xs!!(x-2)) + (xs!!(x-1))
fibonaccisLessThan = takeWhile(<40) $ foldr fibonacci [] [0..]
sumOfEvenFibonaccis = sum $ filter even $ fibonaccisLessThan
main = putStrLn $ show $ sumOfEvenFibonaccis
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么吗?
我正在为我编写的二进制搜索函数编写测试。
module Tests where
import Data.List (sort)
import Test.QuickCheck
import BinarySearch (binarySearch)
prop_equals_elem x xs = (binarySearch x $ sort xs) == (x `elem` xs)
args = Args {replay = Nothing, maxSuccess = 200, maxDiscard=200, maxSize=200, chatty = False}
main = do
quickCheck (prop_equals_elem :: (Ord a) => a -> [a] -> Bool)
Run Code Online (Sandbox Code Playgroud)
在 ghci 中使用 quickCheck 效果很好,但是当我尝试运行 main 时,它给出了错误
Tests.hs:12:5:
Ambiguous type variable `a0' in the constraints:
(Arbitrary a0) arising from a use of `quickCheckWith'
at Tests.hs:12:5-18
(Show a0) …
Run Code Online (Sandbox Code Playgroud) 我试图在python中实现A*算法,但在尝试查找此映射的路径时遇到了问题:
X X X X X X X S = Start
0 0 0 X 0 0 0 E = End
0 S 0 X 0 E 0 X = Wall
0 0 0 X 0 0 0
0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我正在使用曼哈顿方法.我的实现确实找到了一条路径,但不是最短路径.错误从第二步开始 - 向右移动后开始.此时它可以向上移动,启发式成本将是四(三个右,一个下)或下(三个右,一个上).有没有办法让它选择下来获得最短的路径?
码:
class Node:
def __init__(self, (x, y), g, h, parent):
self.x = x
self.y = y
self.g = g
self.h = h
self.f = g+h
self.parent = parent
def __eq__(self, other):
if …
Run Code Online (Sandbox Code Playgroud) 我在Haskell写了一个数独求解器.它通过一个列表,当它找到'0'(一个空单元格)时,它将获得可以适合的数字并尝试它们:
import Data.List (group, (\\), sort)
import Data.Maybe (fromMaybe)
row :: Int -> [Int] -> [Int]
row y grid = foldl (\acc x -> (grid !! x):acc) [] [y*9 .. y*9+8]
where y' = y*9
column :: Int -> [Int] -> [Int]
column x grid = foldl (\acc n -> (grid !! n):acc) [] [x,x+9..80]
box :: Int -> Int -> [Int] -> [Int]
box x y grid = foldl (\acc n -> (grid !! n):acc) [] [x+y*9*3+y' | y' …
Run Code Online (Sandbox Code Playgroud) haskell ×4
a-star ×1
algorithm ×1
optimization ×1
performance ×1
python ×1
quickcheck ×1
sudoku ×1