我在这里有一个函数,用于查看元组列表,并通过获取第一个值来查找元组中的第二个值.这是迄今为止的功能:
lookup :: String -> [(String,String)] -> String
lookup _ _ [] = "Not found"
lookup x y zs = if (notFound x zs)
then "Not found"
else (head [b | (a,b) <- zs, (a==x)])
Run Code Online (Sandbox Code Playgroud)
如果没有包含给定第一个字符串的元组,则notFound函数只返回一个Bool为true.问题是,我在Hugs中遇到这种类型的错误:
ERROR "find.hs" (line 22): Type error in explicitly typed binding
*** Term : lookup
*** Type : String -> [(String,String)] -> [a] -> String
*** Does not match : String -> [(String,String)] -> String
Run Code Online (Sandbox Code Playgroud)
我认为它与虚拟的"未找到"值有关,它与生成列表中的字符串具有不同的类型,但我不确定.
我希望标题听起来不太主观; 我绝对不是要开始就OO进行辩论.我只想讨论解决以下问题的不同方法的基本利弊.
让我们来看一个这个最小的例子:你想表达一个抽象数据类型T,其函数可以将T作为输入,输出或两者:
我想避免向下转发和任何其他动态类型.我也想尽可能避免变异.
abstract class T {
abstract int f1();
// We can't have abstract constructors, so the best we can do, as I see it, is:
abstract void f2(string s);
// The convention would be that you'd replace calls to the original f2 by invocation of the nullary constructor of the implementing type, followed by invocation …Run Code Online (Sandbox Code Playgroud) 我有一个搜索树,定义为:
data (Ord a) => Stree a = Null | Fork (Stree a) a (Stree a) deriving Show
Run Code Online (Sandbox Code Playgroud)
我必须定义两个函数,mapStree:
mapStree :: (Ord b, Ord a) => (a -> b) -> Stree a -> Stree b
Run Code Online (Sandbox Code Playgroud)
和foldStree:
foldStree :: (Ord a) => (b -> a -> b -> b) -> b -> Stree a -> b
Run Code Online (Sandbox Code Playgroud)
我不完全了解发生了什么,也不知道该怎么做.
checkstring :: [String] -> Int -> [String]
checkstring p n = do z <- doesFileExist (p !! n)
if z
then p
else error $ "'" ++ (p !! n) ++ "' file path does not exist"
Run Code Online (Sandbox Code Playgroud)
它通过查看"n"检查字符串中的元素(因此,如果n = 2,它将检查列表中的第二个字符串),然后查看它是否存在.如果它确实存在,它将返回原始字符串列表,如果不存在则会出错.为什么这样做?:
Couldn't match expected type `[t0]' with actual type `IO Bool'
In the return type of a call of `doesFileExist'
In a stmt of a 'do' expression: z <- doesFileExist (p !! n)
Run Code Online (Sandbox Code Playgroud) by functions如何复制列表的每个元素两次.例如,重复[1,3,5]应该返回[1,1,3,3,5,5]?
并用列表中的其他元素替换元素.例如,替换3 30 [1, 3 ,4 ,5, 3, 4]应返回[1, 30, 4, 5, 30, 4]
我在Haskell很新,需要今天提交作业.
任何帮助将不胜感激 !
在过去的几天里,我一直在努力学习Haskell.虽然我正在慢慢变好,但我发现很难用Haskell的IO来推理,可能是因为我缺乏知识.我一直在尝试编写一个简单的待办事项列表程序.这是我得到的:
tadd todo = do
td <- getLine
td:todo
tdel todo = do
trem <- getLine
let rid = read trem :: Int
[todo !! x | x <- [0..(length todo-1)], not $ x == rid]
tls todo = do
mapM putStrLn [ (show x) ++ (todo !! x) | x <- [0..(length todo -1)] ]
todo
mtodo "add" todo = tadd todo
mtodo "del" todo = tdel todo
mtodo "ls" todo = tls todo
bege = do
com <- …Run Code Online (Sandbox Code Playgroud) 我如何给出包含以下所有表达式的一般规则?例如一个表达式,另一个用于sub,一个用于mult.我需要使用递归,但我感到困惑......
simplify :: Expr->Expr
simplify (Mult (Const 0)(Var"x"))
= Const 0
simplify (Mult (Var "x") (Const 0))
= Const 0
simplify (Plus (Const 0) (Var "x"))
= Var "x"
simplify (Plus (Var "x") (Const 0))
= Var "x"
simplify (Mult (Const 1) (Var"x"))
= Var "x"
simplify (Mult(Var"x") (Const 1))
= Var "x"
simplify (Minus (Var"x") (Const 0))
= Var "x"
simplify (Plus (Const x) (Const y))
= Const (x + y)
simplify (Minus (Const x) (Const y))
= Const …Run Code Online (Sandbox Code Playgroud) 我必须创建一个函数,它给出一个键(作为a String),一个值(作为a String)和一个键和值的关联列表(as [(String, String)]).该函数用于将键/值对添加到列表的末尾,如果键已经存在于具有关联值的列表中,则删除旧值.
我已经尝试使用lookup键和关联列表,但我不知道如何处理输出 - lookup函数的输出类型是Maybe String,我似乎无法对其执行列表函数(如删除元素) .有什么方法可以查看列表并删除任何具有给定键的列表元素,而不知道与之关联的值?
我的作业是提供一个函数来计算'x ^ y mod n' - 任何n <(sqrt maxint32)
所以我开始写这样做:
modPow :: Int -> Int -> Int -> Int
modPow x y n = (x `mod` n) ^ (y `mod` n) `mod` n
Run Code Online (Sandbox Code Playgroud)
对于任何数量的n,这似乎工作正常,虽然我的下一个作业问题涉及使用x ^ n mod n = x(Camichael数字),我永远无法让modPow工作.
所以我使用伪代码进行另一个modPow进行mod取幂,来自维基百科:
modPow2 :: Int -> Int -> Int -> Int
modPow2 x y n
= loopmod 1 1
where
loopmod count total = if count > y
then total
else loopmod (count+1) ((total*x) `mod` n)
Run Code Online (Sandbox Code Playgroud)
现在正确地为我的下一个问题产生正确答案,(x ^ n mod n …
我需要定义这个名为total的函数.
total :: (Int -> Int) -> Int -> Int
Run Code Online (Sandbox Code Playgroud)
因此总f是在值n给出和f0 + f1 + .... + fn的函数
谢谢你的帮助!请.
根据这本书作为一个例子,我发现了关于功能组成:
twice f = (f . f)
Run Code Online (Sandbox Code Playgroud)
这里,f是一个函数,结果是f由它自己组成.为此,它需要具有相同的输入和输出类型.所以我们有
twice :: (a -> a) -> a -> a
Run Code Online (Sandbox Code Playgroud)
这表明两次接受一个参数,类型(a - > a)的函数,并返回相同类型的结果.例如,如果successor是将一个添加到整数的函数,
successor :: Int -> Int
successor n = n + 1
then
(twice successor) 12 ->(successor . successor) 12
-> successor (successor 12) -> 14
Run Code Online (Sandbox Code Playgroud)