小编Muf*_*ffy的帖子

如何找到排列的索引

% 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?

permutation prolog

3
推荐指数
1
解决办法
343
查看次数

Haskell - 每个节点中有两个变量的树

我正在尝试定义一个树,每个节点中有两个值.一个表示节点的值,第二个表示距叶子的距离.

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.

tree haskell

2
推荐指数
1
解决办法
306
查看次数

为Digits定义concat函数

我有一个列表字符,我想连接所有字符,这些字符是数字,并且彼此相邻

例如: ['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)

haskell

2
推荐指数
1
解决办法
109
查看次数

标签 统计

haskell ×2

permutation ×1

prolog ×1

tree ×1