% index(+List, -Idx) Predicate will get List with permutation and I want to
know index of permutation
For example: ?- index([4,1,3,2],X).
X= 19.
Run Code Online (Sandbox Code Playgroud)
我的解决方案:
index([],0).
index([_],1).
index([X,Y],2):- Y > X.
index([H,X|T],Idx):-index([X|T],Idx+1),H > X.
Run Code Online (Sandbox Code Playgroud)
为什么错了?我怎样才能增加Idx?
我正在尝试定义一个树,每个节点中有两个值.一个表示节点的值,第二个表示距叶子的距离.
data Tree2 a b = Nil 0 | T (Tree2 a b) (a b) (Tree2 a b) deriving (Eq,Ord,Show,Read)
Run Code Online (Sandbox Code Playgroud)
定义是否正确?"Nil 0"有意义吗?我想说,如果值不在树中而不是距离为0.
我有一个列表字符,我想连接所有字符,这些字符是数字,并且彼此相邻
例如: ['1','5','+','2','4'] => ["15","+","24"]
concat1 :: [Char] -> [Char] -> [String]
concat1 [] [] = []
concat1 [a] [b]
| (isDigit a) && (isDigit b) = [a] ++ [b]
Run Code Online (Sandbox Code Playgroud)
我试着编写这段代码,但似乎没有正确的方法,调试器告诉我这个:
Couldn't match type `Char' with `[Char]'
Expected type: [String]
Actual type: [Char]
* In the expression: [a] ++ [b]
In an equation for `concat1':
concat1 [a] [b] | (isDigit a) && (isDigit b) = [a] ++ [b]
Run Code Online (Sandbox Code Playgroud)