我想做一个Haskell函数,其中输入(字符串列表)是有序的(总是.输入只有在有序时才有效),我想得到每个不同字符串的出现次数.
例:
ContaOcs["a", "a", "b", "c", "c", "c", "d"]
Run Code Online (Sandbox Code Playgroud)
应该返回:
[(2,"a"), (1,"b"), (3,"c"), (1,"d")]
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试做的事情:
module Main where
contaOcs :: [String] -> [(Int, String)]
contaOcs [] = [_,_]
contaOcs [x] = [1,x]
contaOcs (i, x1:x2:xs)
| x1 == x2 = (i+1,(x2:xs))
| otherwise = (0, (x2:xs))
Run Code Online (Sandbox Code Playgroud)
但是这段代码有一些错误,我不太清楚我应该怎样做才能实现这一点我是功能编程和Haskell的新手.任何人都可以帮我一些信息吗?
谢谢你的帮助.
我正在从指针执行转换然后它让我继续运行此警告(赋值使得指针来自整数而没有强制转换).这是代码:
#include<stdio.h>
#include<stdbool.h>
typedef int TipoChave;
typedef struct TipoRegistro {
TipoChave Chave;
/*outros componentes*/
} TipoRegistro;
typedef struct TipoPagina* TipoApontador;
typedef struct TipoPagina {
int registros;
TipoRegistro *r;
TipoApontador *p;
} TipoPagina;
TipoApontador NovaSubArvore(int ordem){
TipoApontador A;
A=malloc(sizeof(TipoPagina));
int i;
A->registros=0;
A->r=malloc((2*ordem)*sizeof(TipoRegistro));
A->p=malloc((2*ordem+1)*sizeof(TipoPagina));
for (i=0;i<(2*ordem+1);i++){
A->p[i]=NULL;
if(i!=2*ordem){
A->r[i].Chave=0;
}
}
return (A);
}
Run Code Online (Sandbox Code Playgroud)
在主要我打电话:
TipoApontador Raiz;
Run Code Online (Sandbox Code Playgroud)
然后:
Raiz=NovaSubArvore(ordem); //Warning happens here
Run Code Online (Sandbox Code Playgroud)
如果我做:
if (Raiz!=NULL)
free(Raiz);
Run Code Online (Sandbox Code Playgroud)
它运行一个免费的invallid(奇怪,因为如果Raiz是NULL,免费应该没有运行.请问有人帮我解决这个问题吗?我认为这个警告是让我免于"解放"的问题.
编辑:关于waring解决的确定问题.但是如果我做了2次免费游戏就会运行一个无效的免费游戏(我有一个免费的东西,其他时间没有.如果我自由地执行"if(Raiz!= NULL)"应该阻止另一个免费的跑步.但事实并非如此.