Char -> String -> String 类型的函数 f ,它将一个字符串和一个字符作为输入,并返回一个字符串,其中所有出现的字符都是重复的。示例:f 'o' “Hello world!” 应该返回“Hello world!”。
Hjo
Asked:
2023-04-22 18:29:53 +0800 CST
我想做一个案例,它将为某个数字 n 给出一个结果 (42),为 n + 1 给出另一个结果 (43),为其他所有结果给出第三个结果 (44)。我正在尝试这样做:
f x = let n = 4 in case x of {n -> 42; n+1 -> 43; _ -> 44}
不起作用。我认为是因为您不能在模式中使用表达式 (n+1)。我正在尝试通过警卫:
f x = let n=4 in case x of {n -> 42; _ | x == n+1 -> 43 | True -> 44}
也不行。写一个额外的模式。如何使最短的权利?
sram
Asked:
2023-04-18 14:29:56 +0800 CST
给定一组点X
,Y
在字符串的新行上,分别获得两个列表X
和Y
一个列表的最短方法是什么?或者,有什么相同之处 - 有 1 个列表,如何获得两个元素位于偶数和奇数位置的列表?
"1 2\n3 4\n" -> [[1, 3], [2, 4]]
[1, 2, 3, 4] -> [[1, 3], [2, 4]]
从 1 个输入,您可以通过 获得第二个words
。对于第二个输入,你可以做
splitOnPos [] = [[], []]
splitOnPos (x1:x2:xs) = [x1:odds, x2:evens]
where [odds, evens] = splitOnPos xs
但是也许您可以以某种方式缩短第一个的时间?或者缩短第二个的解决方案?可以以某种方式同时head
和last
之后申请吗map words . lines
?
D-SK Ch
Asked:
2022-07-29 19:47:04 +0800 CST
无法按高度比较具有大于 (>) 和大于或等于 (>=) 符号的树。这是我的代码。只是在 tree1 >= tree2 命令时挂起
data Tree a = Node a [Tree a] deriving (Eq,Show)
instance (Ord a) => Ord (Tree a) where
Node a list1 >= Node b list2 = (list1 > list2) || (list1 == list2)
(Node a list1) > (Node b list2) = (list1 > list2)
t0 = Node 15 []
t1 = Node 15 []
t2 = Node 120 [t0]
tree1 = Node 130 [t1,t2]
tt0 = Node 155 []
tt1 = Node 112 [t2]
tt2 = Node 128 [t0]
tree2 = Node 110 [tt1,tt2,tt0]
Rikitikitavi
Asked:
2022-03-26 23:41:50 +0800 CST
有这段代码,它试图描述一个机器人对象:
robot (name, attack, hp) = \message -> message(name, attack, hp)
name (n, _, _) = n
attack (_, a, _) = a
hp (_, _, hp) = hp
getAttack aRobot = aRobot attack
getHP aRobot = aRobot hp
getName aRobot = aRobot name
setName aRobot newName = aRobot(\(n, a, h) -> robot(newName, a , h))
setAttack aRobot newAttack = aRobot(\(n, a, h) -> robot(n, newAttack , h))
setHP aRobot newHP = aRobot(\(n, a, h) -> robot(n, n , newHP))
printRobot aRobot = aRobot( \(n, a, h) -> n ++ " attack: " ++ (show a) ++ " hp: " ++ (show h) )
damage aRobot dmg = aRobot(\(n,a,h) -> robot(n,a, h - dmg))
fight aRobot defender = damage defender dmg
where dmg = if getHP aRobot > 0
then getAttack aRobot
else 0
getAllHP aRobots = map getHP aRobots
现在我需要编写一个函数,在一轮后返回两个机器人的元组,我这样做:
oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
一个错误:
*Main> :l robot.hs
[1 of 1] Compiling Main ( robot.hs, interpreted )
robot.hs:27:48: error:
* Occurs check: cannot construct the infinite type:
b2 ~ ((a2, b2, b1) -> t1) -> t1
Expected type: ((a2, ((a2, b2, b1) -> t1) -> t1,
((a2, b2, b1) -> t1) -> t1)
-> ((a2, b2, b1) -> t1) -> t1)
-> a1
Actual type: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
* In the first argument of `fight', namely `aR2'
In the expression: fight aR2 aR1
In the expression: (fight aR1 aR2, fight aR2 aR1)
* Relevant bindings include
aR2 :: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
(bound at robot.hs:27:19)
aR1 :: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
(bound at robot.hs:27:15)
oneRoundFight :: (((a, ((a, b, a1) -> t) -> t,
((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1)
-> (((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1)
-> (a1, b1)
(bound at robot.hs:27:1)
|
27 | oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
| ^^^
robot.hs:27:52: error:
* Occurs check: cannot construct the infinite type:
b ~ ((a, b, a1) -> t) -> t
Expected type: ((a, b, a1) -> ((a, b, a1) -> t) -> t) -> b1
Actual type: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
* In the second argument of `fight', namely `aR1'
In the expression: fight aR2 aR1
In the expression: (fight aR1 aR2, fight aR2 aR1)
* Relevant bindings include
aR2 :: ((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1
(bound at robot.hs:27:19)
aR1 :: ((a, ((a, b, a1) -> t) -> t, ((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1
(bound at robot.hs:27:15)
oneRoundFight :: (((a, ((a, b, a1) -> t) -> t,
((a, b, a1) -> t) -> t)
-> ((a, b, a1) -> t) -> t)
-> b1)
-> (((a2, b2, b1) -> ((a2, b2, b1) -> t1) -> t1) -> a1)
-> (a1, b1)
(bound at robot.hs:27:1)
|
27 | oneRoundFight aR1 aR2 = ( fight aR1 aR2, fight aR2 aR1 )
| ^^^
Failed, no modules loaded.
Prelude>
我不太明白这个输出是什么意思,特别是cannot construct the infinite type:
,如果你改变地方的参数:( fight aR1 aR2, fight aR1 aR2 )
那么这个错误就不会发生。
你能解释一下吗?
同时,函数fight
编写并工作:
Prelude> :l robot.hs
[1 of 1] Compiling Main ( robot.hs, interpreted )
Ok, one module loaded.
*Main> aR1 = robot("r1", 8, 100)
*Main> aR2 = robot("r2", 4, 120)
*Main> aR2_damaged = fight aR1 aR2
*Main> aR1_damaged = fight aR2 aR1
*Main> printRobot aR1_damaged
"r1 attack: 8 hp: 96"
*Main> printRobot aR2_damaged
"r2 attack: 4 hp: 112"