Haskell 按长度过滤

Haskell 按长度过滤,haskell,Haskell,如何在这里设置过滤器(x:xs)=(x,length(x:xs)),当length>1时设置length 目前,如果输入是abcaaabb输出是[('a',1),('b',1),('c',1),('a',3),('b',3)],但我正在寻找abca3b3 我的代码: import Data.List encode :: [Char] -> [(Char, Int)] encode s = map go (group s) where go (x:xs) =

如何在这里设置过滤器
(x:xs)=(x,length(x:xs))
,当length>1时设置length

目前,如果输入是
abcaaabb
输出是
[('a',1),('b',1),('c',1),('a',3),('b',3)]
,但我正在寻找
abca3b3

我的代码:

import Data.List

encode :: [Char] -> [(Char, Int)]
encode s = map go (group s)
               where go (x:xs) = (x, length (x:xs))

main = do
    s <- getLine
    print (encode s) 
导入数据。列表
编码::[Char]->[(Char,Int)]
编码s=地图转到(s组)
其中go(x:xs)=(x,长度(x:xs))
main=do

s x:[y])(编码s))
用于将列表转换为字符串。

在Haskell中不能有这样的列表:

[('a'),('b'),('c'),('a',3),('b',3)]
如果列表中的每个元素需要在haskell中具有相同的类型,并且('c')[('a')::Char]和('b',3)[('a',1::Num t=>(Char,t)]是不同的类型

也许还可以看看

我建议您将列表更改为(Char,也许num)数据结构

编辑:

从你的新问题来看,我认为你一直在寻找:

import Data.List

encode :: [Char] -> [(Char, Int)]
encode s = map go (group s)
               where go (x:xs) = (x, length (x:xs))

f :: (Char, Int) -> String
f (a, b) = if b == 1 then [a] else [a] ++ show b

encode2 :: [(Char, Int)] -> String
encode2 [] = []
encode2 (x:xs) = f(x) ++ encode2 xs

main = do
    s <- getLine
    putStrLn $ encode2 $ encode s
导入数据。列表
编码::[Char]->[(Char,Int)]
编码s=地图转到(s组)
其中go(x:xs)=(x,长度(x:xs))
f::(字符,Int)->字符串
f(a,b)=如果b==1,那么[a]其他[a]++显示b
编码2::[(字符,整数)]->字符串
encode2[]=[]
encode2(x:xs)=f(x)++encode2xs
main=do

s在Haskell中不能有这样的列表:

[('a'),('b'),('c'),('a',3),('b',3)]
如果列表中的每个元素需要在haskell中具有相同的类型,并且('c')[('a')::Char]和('b',3)[('a',1::Num t=>(Char,t)]是不同的类型

也许还可以看看

我建议您将列表更改为(Char,也许num)数据结构

编辑:

从你的新问题来看,我认为你一直在寻找:

import Data.List

encode :: [Char] -> [(Char, Int)]
encode s = map go (group s)
               where go (x:xs) = (x, length (x:xs))

f :: (Char, Int) -> String
f (a, b) = if b == 1 then [a] else [a] ++ show b

encode2 :: [(Char, Int)] -> String
encode2 [] = []
encode2 (x:xs) = f(x) ++ encode2 xs

main = do
    s <- getLine
    putStrLn $ encode2 $ encode s
导入数据。列表
编码::[Char]->[(Char,Int)]
编码s=地图转到(s组)
其中go(x:xs)=(x,长度(x:xs))
f::(字符,Int)->字符串
f(a,b)=如果b==1,那么[a]其他[a]++显示b
编码2::[(字符,整数)]->字符串
encode2[]=[]
encode2(x:xs)=f(x)++encode2xs
main=do

s在Haskell中不能有这样的列表:

[('a'),('b'),('c'),('a',3),('b',3)]
如果列表中的每个元素需要在haskell中具有相同的类型,并且('c')[('a')::Char]和('b',3)[('a',1::Num t=>(Char,t)]是不同的类型

也许还可以看看

我建议您将列表更改为(Char,也许num)数据结构

编辑:

从你的新问题来看,我认为你一直在寻找:

import Data.List

encode :: [Char] -> [(Char, Int)]
encode s = map go (group s)
               where go (x:xs) = (x, length (x:xs))

f :: (Char, Int) -> String
f (a, b) = if b == 1 then [a] else [a] ++ show b

encode2 :: [(Char, Int)] -> String
encode2 [] = []
encode2 (x:xs) = f(x) ++ encode2 xs

main = do
    s <- getLine
    putStrLn $ encode2 $ encode s
导入数据。列表
编码::[Char]->[(Char,Int)]
编码s=地图转到(s组)
其中go(x:xs)=(x,长度(x:xs))
f::(字符,Int)->字符串
f(a,b)=如果b==1,那么[a]其他[a]++显示b
编码2::[(字符,整数)]->字符串
encode2[]=[]
encode2(x:xs)=f(x)++encode2xs
main=do

s在Haskell中不能有这样的列表:

[('a'),('b'),('c'),('a',3),('b',3)]
如果列表中的每个元素需要在haskell中具有相同的类型,并且('c')[('a')::Char]和('b',3)[('a',1::Num t=>(Char,t)]是不同的类型

也许还可以看看

我建议您将列表更改为(Char,也许num)数据结构

编辑:

从你的新问题来看,我认为你一直在寻找:

import Data.List

encode :: [Char] -> [(Char, Int)]
encode s = map go (group s)
               where go (x:xs) = (x, length (x:xs))

f :: (Char, Int) -> String
f (a, b) = if b == 1 then [a] else [a] ++ show b

encode2 :: [(Char, Int)] -> String
encode2 [] = []
encode2 (x:xs) = f(x) ++ encode2 xs

main = do
    s <- getLine
    putStrLn $ encode2 $ encode s
导入数据。列表
编码::[Char]->[(Char,Int)]
编码s=地图转到(s组)
其中go(x:xs)=(x,长度(x:xs))
f::(字符,Int)->字符串
f(a,b)=如果b==1,那么[a]其他[a]++显示b
编码2::[(字符,整数)]->字符串
encode2[]=[]
encode2(x:xs)=f(x)++encode2xs
main=do

因为我自己也是一个新手,这可能不是哈斯凯尔式的。但是你可以这样做(xs是列表
[('a',1),('b',2),('a',3)]
):

创建“a1b2a3”:

过滤掉1s:

filter (\x -> x /= '1') "a1b2a3"

我会给你“ab2a3”

因为我自己也是个新手,这可能不是哈斯凯尔式的。但是你可以这样做(xs是列表
[('a',1),('b',2),('a',3)]
):

创建“a1b2a3”:

过滤掉1s:

filter (\x -> x /= '1') "a1b2a3"

我会给你“ab2a3”

因为我自己也是个新手,这可能不是哈斯凯尔式的。但是你可以这样做(xs是列表
[('a',1),('b',2),('a',3)]
):

创建“a1b2a3”:

过滤掉1s:

filter (\x -> x /= '1') "a1b2a3"

我会给你“ab2a3”

因为我自己也是个新手,这可能不是哈斯凯尔式的。但是你可以这样做(xs是列表
[('a',1),('b',2),('a',3)]
):

创建“a1b2a3”:

过滤掉1s:

filter (\x -> x /= '1') "a1b2a3"

将为您提供“ab2a3”

不确定这是否适合您的需要,但如果您不需要过滤,这将完成以下工作:

encode::String -> String
encode "" = ""
encode (x:xs) = doIt0 xs x 1 where
        doIt0 [] ch currentPos = [ch]++showPos currentPos
        doIt0 (x:xs) ch currentPos
                |x==ch = doIt0 xs ch $ currentPos+1
                |otherwise= [ch]++ (showPos currentPos) ++ (doIt0 xs x 1)
        showPos pos = if pos> 1 then show pos else ""

main = do
        s <- getLine
        print (encode s)                
encode::String->String
编码“”=“”
encode(x:xs)=doIt0 xs x 1,其中
doIt0[]ch currentPos=[ch]++showPos currentPos
doIt0(x:xs)ch currentPos
|x==ch=doIt0 xs ch$currentPos+1
|否则=[ch]++(showPos currentPos)++(doIt0 xs x 1)
showPos pos=如果pos>1,则显示pos else“”
main=do

s不确定这是否适合您的需要,但如果您不需要过滤,则可以这样做:

encode::String -> String
encode "" = ""
encode (x:xs) = doIt0 xs x 1 where
        doIt0 [] ch currentPos = [ch]++showPos currentPos
        doIt0 (x:xs) ch currentPos
                |x==ch = doIt0 xs ch $ currentPos+1
                |otherwise= [ch]++ (showPos currentPos) ++ (doIt0 xs x 1)
        showPos pos = if pos> 1 then show pos else ""

main = do
        s <- getLine
        print (encode s)                
encode::String->String
编码“”=“”
encode(x:xs)=doIt0 xs x 1,其中
doIt0[]ch currentPos=[ch]++showPos currentPos
doIt0(x:xs)ch currentPos
|x==ch=doIt0 xs ch$currentPos+1
|否则=[ch]++(showPos currentPos)++(doIt0 xs x 1)
showPos pos=如果pos>1,则显示pos else“”
main=do

s不确定这是否适合您的需要,但如果您不需要过滤,则可以这样做:

encode::String -> String
encode "" = ""
encode (x:xs) = doIt0 xs x 1 where
        doIt0 [] ch currentPos = [ch]++showPos currentPos
        doIt0 (x:xs) ch currentPos
                |x==ch = doIt0 xs ch $ currentPos+1
                |otherwise= [ch]++ (showPos currentPos) ++ (doIt0 xs x 1)
        showPos pos = if pos> 1 then show pos else ""

main = do
        s <- getLine
        print (encode s)                
encode::String->String
编码“”=“”
encode(x:xs)=doIt0 xs x 1,其中
doIt0[]ch currentPos=[ch]++showPos currentPos
doIt0(x:xs)ch currentPos
|x==ch=doIt0 xs ch$currentPos+1
|否则=[ch]++(showPos currentPos)++(doIt0 xs x 1)
showPos pos=如果pos>1,则显示pos else“”
main=do

s不确定这是否适合您的需要,但如果您不需要