我正在尝试使用此处的代码创建和使用字典:
import Data.List (lookup)
insert :: Eq a => (a,b) -> [(a,b)] -> [(a,b)]
insert (a,b) [] = [(a,b)]
insert (a,b) ((c,d):rest) = if a == c
then (a,b) : rest
else (c,d) : insert (a,b) rest
dict :: [(String, String)]
dict = [("", "")]
main = do
insert ("onekey", "onevalue") dict
print dict
print $ lookup "onekey" dict
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
$ runghc rndict.hs
rndict.hs:22:1: error:
• Couldn't match expected type ‘IO t0’ with actual type ‘[()]’
• In …Run Code Online (Sandbox Code Playgroud) dictionary haskell compiler-errors type-mismatch do-notation
试图将Haskell函数转换为Clojure。但是面临困难。不知道发生了什么。
这是递归的Haskell函数。
mapWidth :: [[Char]] -> Int
mapWidth [] = 0
mapWidth (x:xs)
| length xs == 0 = length x
| length x /= length (xs!!0) = -1
| otherwise = mapWidth(xs)
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我尝试过的:
(defn mapWidth [data_list]
(def data 0)
([[x & xs](seq data_list)](if (= (count x) 0)
(data 0)
(data -1))))
([[x & xs](seq data_list)](if not(= (count xs) length (xs!!0))
(data 0)
(data -1)
mapWidth(xs)))
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。我对这两种语言都是新手。
所以我有这些常量FilePath变量(字符串)
s1 , s2 , s3 , s4 ... :: Filepath
s1 = "help.txt"
s2 = "sljdfn"
-- ...
Run Code Online (Sandbox Code Playgroud)
而且我有一个函数接受这些文件路径之一并返回一个int值。
positionInList:: Filepath -> Int
positionInList s1 = 1
positionInList s2 = 2
-- ...
Run Code Online (Sandbox Code Playgroud)
但是,在编译时会出现模式匹配冗余警告,并且程序运行异常,所以我认为这是问题所在。我将如何解决呢?
我正在尝试实现一个消除字符串空格的函数。如果谓词仅显式过滤空格,我不明白为什么此列表推导式会消除第一个字符。
import Data.List
import System.IO
noSpace :: String -> String
noSpace (x:xs) = [x | x <- xs, x /= ' ']
main = do
print(noSpace("8 j 8 mBliB8g imjB8B8 jl B"))
Run Code Online (Sandbox Code Playgroud)
结果:
"j8mBliB8gimjB8B8jlB"
Run Code Online (Sandbox Code Playgroud)
应该:
8j8mBliB8gimjB8B8jlB
Run Code Online (Sandbox Code Playgroud) 我知道这个问题之前已经被回答过,但我不太明白对该问题的解释。
我在 HackerRank 上做了 30 天的代码,其中一个练习是检查一个数字是否是素数。不幸的是,我自己无法做到这一点,所以我在多次尝试后检查了给定的解决方案。即使在查看了解决方案之后,我也无法理解其中一行:
// Check for primality using odd numbers from 3 to sqrt(n)
for(int i = 3; i <= sqrt(n); i += 2){
// n is not prime if it is evenly divisible by some 'i' in this range
if( n % i == 0 ){
isPrime = false;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么sqrt(n)在for循环中使用?
根据我的理解,Haskell 使用惰性求值,它允许在有限的时间内对例如无限列表的操作进行求值。
作为测试,我定义了以下函数
X Boolean
Y Int
f(X,Y) = (Y == 3) OR X
Run Code Online (Sandbox Code Playgroud)
因此, fold left 应用于[1..]具有False初始值的无限整数列表和上面定义的函数,应该返回True,因为当它达到n=3评估时f(n==3,False)将返回True,因此这True将通过函数传播。
我在 Haskell 代码中实现了这个功能
myfunc :: Bool -> Int -> Bool
myfunc True _ = True
myfunc _ n
| (n == 3) = True
| otherwise = False
Run Code Online (Sandbox Code Playgroud)
并在 cli 中试用
foldl myfunc False [1..]
Run Code Online (Sandbox Code Playgroud)
该命令变得无响应,表明它正在执行无限计算。为什么 Haskell 没有从这里的惰性求值中受益?
我有这个程序,它只是打印出命令行参数。
echoArgs :: IO ()
echoArgs = do
line <- getArgs
print line
Run Code Online (Sandbox Code Playgroud)
我想知道的是为什么当我输入时会失败:
echoArgs :: IO ()
echoArgs = do
line <- getArgs
putStrLn line
Run Code Online (Sandbox Code Playgroud)
以及为什么当我将其更改为以下内容时它不起作用:
echoArgs :: IO String
echoArgs = do
line <- getArgs
let line' = read line :: String
putStrLn line'
Run Code Online (Sandbox Code Playgroud) 我完全是 Haskell 的初学者,我来自 js 环境,我有一个简单的数组students,我想将一些学生对象推入其中,但遗憾的是 Haskell 不支持对象(如果有我可以做到的方法,请指导我)所以尝试制作一个读取用户输入(数组)并将其推入students数组的简单程序,这是我尝试过的:
main :: IO()
main = do
let students = []
studentArray <- getLine
students ++ studentArray
print(students)
Run Code Online (Sandbox Code Playgroud)
但抛出以下错误: Couldn't match type `[]' with `IO'
我有以下代码:
(define numbers '(2 3 5 3 1 22 2))
(define (count val l)
(if (null? l)
0
(+
(if (= (first l) val) 1 0)
(count val (rest l))
)
)
)
(display (count 6 numbers))
Run Code Online (Sandbox Code Playgroud)
(对不起,如果我的代码看起来很糟糕,只需要使用这种语言一次)
编译器说:
count: contract violation
expected: procedure?
given: 6
argument position: 1st
other arguments...:
'(3 5 3 1 22 2)
Run Code Online (Sandbox Code Playgroud) 我有用于我尚未编写的 Haskell 应用程序的输入数据,这些数据驻留在一个文件中。我不更新文件。我只需要读取文件并将其输入到需要字符串列表的 Haskell 函数中。但是读取文件当然会产生IO数据对象。我了解到使用该<-操作可以以某种方式“取出”打包在IO结构中的字符串,因此我尝试了以下尝试:
run :: [String]
run = do
datadef_content <- readFile "play.txt" -- yields a String
let datadef = lines datadef_content -- should be a [String]
return datadef
Run Code Online (Sandbox Code Playgroud)
我把它放到一个文件中play.hs,然后从 ghci 加载它
:l play
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,我收到了该readFile行的错误消息
Run Code Online (Sandbox Code Playgroud)Couldn't match type ‘IO’ with ‘[]’ Expected type: [String] Actual type: IO String
和return错误信息
Run Code Online (Sandbox Code Playgroud)Couldn't match type ‘[Char]’ with ‘Char’ Expected type: [String] Actual type: [[String]]
第一条似乎表明我无法摆脱IO,而最后一条消息似乎表明,这lines将返回一个字符串列表列表,这对我来说也没有意义。 …