Haskell 如何获得连续二进制数的字符串?

Haskell 如何获得连续二进制数的字符串?,haskell,Haskell,我如何才能像这样插入迭代函数:显示数据库a intToDigit b”“?我试图使用composition(showinttbase a intToDigit(\x->iterate(+1)0)”“),但它绝对不正确 例如,我试图从无限列表中的十进制数生成二进制数字符串: 以7$myFunc=>[“0”、“01”、“10”、“11”、“100”、“101”、“110”]为例,您希望执行以下步骤: 从n=“0”开始 将n转换为Int 将1添加到nin 将nInt转换为基数为2的String 转至步

我如何才能像这样插入
迭代
函数:
显示数据库a intToDigit b”“
?我试图使用composition(
showinttbase a intToDigit(\x->iterate(+1)0)”“
),但它绝对不正确

例如,我试图从无限列表中的十进制数生成二进制数字符串:
以7$myFunc=>[“0”、“01”、“10”、“11”、“100”、“101”、“110”]

为例,您希望执行以下步骤:

  • n=“0”开始
  • n
    转换为
    Int
  • 1
    添加到
    nin
  • nInt
    转换为基数为2的
    String
  • 转至步骤2
  • 这确实是
    迭代
    函数的一个很好的应用程序;然而,看起来你的函数组合和顺序都搞砸了。我假设您定义了以下函数:

    -- Converts `n` to base `base` as a String
    showIntAtBase :: Int -> Int -> String
    showIntAtBase base n = undefined
    
    -- Converts the String `s` to an `Int` assuming its in base `base`
    readIntBase :: Int -> String -> Int
    readIntBase base s = undefined
    
    以致

    showIntAtBase b . readIntBase b === id
    -- and
    readIntBase b . showIntAtBase b === id
    
    这取决于你去证明这些函数是彼此的倒数,并且工作正常

    然后我将编写一个函数,为我执行步骤2到步骤4

    incrBase2 :: String -> String
    incrBase2 s = showIntAtBase 2 $ (\x -> x + 1) $ readIntBase 2 s
    
    或者,你可以把它写得更简单

    incrBase2 = showIntAtBase 2 . (+1) . readIntBase 2
    
    现在,您可以在RBASE2的
    中使用
    迭代

    allBinary :: [String]
    allBinary = iterate incrBase2 "0"
    

    因此,根据您的示例,您希望执行以下步骤:

  • n=“0”开始
  • n
    转换为
    Int
  • 1
    添加到
    nin
  • nInt
    转换为基数为2的
    String
  • 转至步骤2
  • 这确实是
    迭代
    函数的一个很好的应用程序;然而,看起来你的函数组合和顺序都搞砸了。我假设您定义了以下函数:

    -- Converts `n` to base `base` as a String
    showIntAtBase :: Int -> Int -> String
    showIntAtBase base n = undefined
    
    -- Converts the String `s` to an `Int` assuming its in base `base`
    readIntBase :: Int -> String -> Int
    readIntBase base s = undefined
    
    以致

    showIntAtBase b . readIntBase b === id
    -- and
    readIntBase b . showIntAtBase b === id
    
    这取决于你去证明这些函数是彼此的倒数,并且工作正常

    然后我将编写一个函数,为我执行步骤2到步骤4

    incrBase2 :: String -> String
    incrBase2 s = showIntAtBase 2 $ (\x -> x + 1) $ readIntBase 2 s
    
    或者,你可以把它写得更简单

    incrBase2 = showIntAtBase 2 . (+1) . readIntBase 2
    
    现在,您可以在RBASE2的
    中使用
    迭代

    allBinary :: [String]
    allBinary = iterate incrBase2 "0"
    

    因此,根据您的示例,您希望执行以下步骤:

  • n=“0”开始
  • n
    转换为
    Int
  • 1
    添加到
    nin
  • nInt
    转换为基数为2的
    String
  • 转至步骤2
  • 这确实是
    迭代
    函数的一个很好的应用程序;然而,看起来你的函数组合和顺序都搞砸了。我假设您定义了以下函数:

    -- Converts `n` to base `base` as a String
    showIntAtBase :: Int -> Int -> String
    showIntAtBase base n = undefined
    
    -- Converts the String `s` to an `Int` assuming its in base `base`
    readIntBase :: Int -> String -> Int
    readIntBase base s = undefined
    
    以致

    showIntAtBase b . readIntBase b === id
    -- and
    readIntBase b . showIntAtBase b === id
    
    这取决于你去证明这些函数是彼此的倒数,并且工作正常

    然后我将编写一个函数,为我执行步骤2到步骤4

    incrBase2 :: String -> String
    incrBase2 s = showIntAtBase 2 $ (\x -> x + 1) $ readIntBase 2 s
    
    或者,你可以把它写得更简单

    incrBase2 = showIntAtBase 2 . (+1) . readIntBase 2
    
    现在,您可以在RBASE2的
    中使用
    迭代

    allBinary :: [String]
    allBinary = iterate incrBase2 "0"
    

    因此,根据您的示例,您希望执行以下步骤:

  • n=“0”开始
  • n
    转换为
    Int
  • 1
    添加到
    nin
  • nInt
    转换为基数为2的
    String
  • 转至步骤2
  • 这确实是
    迭代
    函数的一个很好的应用程序;然而,看起来你的函数组合和顺序都搞砸了。我假设您定义了以下函数:

    -- Converts `n` to base `base` as a String
    showIntAtBase :: Int -> Int -> String
    showIntAtBase base n = undefined
    
    -- Converts the String `s` to an `Int` assuming its in base `base`
    readIntBase :: Int -> String -> Int
    readIntBase base s = undefined
    
    以致

    showIntAtBase b . readIntBase b === id
    -- and
    readIntBase b . showIntAtBase b === id
    
    这取决于你去证明这些函数是彼此的倒数,并且工作正常

    然后我将编写一个函数,为我执行步骤2到步骤4

    incrBase2 :: String -> String
    incrBase2 s = showIntAtBase 2 $ (\x -> x + 1) $ readIntBase 2 s
    
    或者,你可以把它写得更简单

    incrBase2 = showIntAtBase 2 . (+1) . readIntBase 2
    
    现在,您可以在RBASE2的
    中使用
    迭代

    allBinary :: [String]
    allBinary = iterate incrBase2 "0"
    
    除了
    “0”
    ,所有数字都以
    1
    开头。让我们只考虑二进制代码的尾部发生什么,从<代码> 1 /代码>:

    ""    -- 1
    "0"   -- 10
    "1"   -- 11
    "00"  -- 100
    "01"  -- 101
    "10"  -- 110
    "11"  -- 111
    
    等等

    我们在这里看到的是一种模式,首先我们将
    0
    附加到上一次迭代结果的头部,然后以相同的方式附加
    1

    ts = concat $ iterate (\xss -> [h:xs | h <-['0','1'], xs <- xss]) [[]]
    
    除了
    “0”
    ,所有数字都以
    1
    开头。让我们只考虑二进制代码的尾部发生什么,从<代码> 1 /代码>:

    ""    -- 1
    "0"   -- 10
    "1"   -- 11
    "00"  -- 100
    "01"  -- 101
    "10"  -- 110
    "11"  -- 111
    
    等等

    我们在这里看到的是一种模式,首先我们将
    0
    附加到上一次迭代结果的头部,然后以相同的方式附加
    1

    ts = concat $ iterate (\xss -> [h:xs | h <-['0','1'], xs <- xss]) [[]]
    
    除了
    “0”
    ,所有数字都以
    1
    开头。让我们只考虑二进制代码的尾部发生什么,从<代码> 1 /代码>:

    ""    -- 1
    "0"   -- 10
    "1"   -- 11
    "00"  -- 100
    "01"  -- 101
    "10"  -- 110
    "11"  -- 111
    
    等等

    我们在这里看到的是一种模式,首先我们将
    0
    附加到上一次迭代结果的头部,然后以相同的方式附加
    1

    ts = concat $ iterate (\xss -> [h:xs | h <-['0','1'], xs <- xss]) [[]]
    
    除了
    “0”
    ,所有数字都以
    1
    开头。让我们只考虑二进制代码的尾部发生什么,从<代码> 1 /代码>:

    ""    -- 1
    "0"   -- 10
    "1"   -- 11
    "00"  -- 100
    "01"  -- 101
    "10"  -- 110
    "11"  -- 111
    
    等等

    我们在这里看到的是一种模式,首先我们将
    0
    附加到上一次迭代结果的头部,然后以相同的方式附加
    1

    ts = concat $ iterate (\xss -> [h:xs | h <-['0','1'], xs <- xss]) [[]]
    

    这些功能的类型是什么?你到底想做什么?也许是一个示例输入和预期输出?@bheklillr我添加了一个示例,我希望现在更清楚这些函数的类型是什么?你到底想做什么?也许是一个示例输入和预期输出?@bheklillr我添加了一个示例,我希望现在更清楚这些函数的类型是什么?你到底想做什么?也许是一个示例输入和预期输出?@bheklillr我添加了一个示例,我希望现在更清楚这些函数的类型是什么?你到底想做什么?也许是一个示例输入和预期输出?@bheklillr我添加了一个示例,我希望它更清楚now@augustss