标签: infinite

无限计数器的无限列表

对于那些有可疑头脑的人来说,这不是家庭作业,只是好奇.

给定一个有限的字母表,是否有可能构建一个由反向词汇顺序的字母表组成的无限长单词列表?

即给出字母表 "ab"

是否可以构建列表:

["aaaaaa...", "baaaaa...", "abaaaa...", "bbaaaa...", "aabaaa...", ...]
Run Code Online (Sandbox Code Playgroud)

where ...表示扩展到无限长度的列表(和列表列表).

一个天真的尝试是:

counters alphabet = [c:ounter | ounter <- counters alphabet, c <- alphabet]
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为它是递归的.

当然,对于工作版本,如果您尝试打印结果,则只会看到第一个元素被打印为字母表中第一个元素的无限列表.但是,您应该能够这样做:

mapM_ (print . take 2) . take 4 . counters $ "ab"
Run Code Online (Sandbox Code Playgroud)

并看到输出:

aa
ba
ab
bb
Run Code Online (Sandbox Code Playgroud)

haskell infinite

10
推荐指数
3
解决办法
470
查看次数

Dijkstra的单源最短路径算法可以在图中检测到无限循环吗?

所以我遇到了这个美丽的问题,要求你编写一个程序,找出有向图中是否存在负无穷短路径.(也可以认为是在图中存在"负循环").这是问题的链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499

我通过从图中的任何源开始两次运行Bellman Ford算法成功地解决了这个问题.我第二次运行算法时,检查节点是否可以放松.如果是这样,那么图中肯定存在负循环.下面是我的C++代码:

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int test;
    cin>>test;

    for(int T=0; T<test; T++)
    {

        int node, E;

        cin>>node>>E; 

        int **edge= new int *[E];
        for(int i=0; i<E; i++)
        {
            edge[i]= new int [3];
            cin>>edge[i][0]>>edge[i][1]>>edge[i][2];
        }

        int *d= new int [node];

        bool possible=false;

        for(int i=0; i<node;i++)
        {
            d[i]= 999999999;
        }

        d[node-1]=0;

        for(int i=0; i<node-1; i++)
        {

            for(int j=0; j<E; j++)
            {
                if(d[edge[j][1]]>d[edge[j][0]]+edge[j][2])
                    d[edge[j][1]]=d[edge[j][0]]+edge[j][2];
            }
        }

        // time to judge!
        for(int i=0; i<node-1; i++)
        {

            for(int …
Run Code Online (Sandbox Code Playgroud)

algorithm dijkstra infinite shortest-path bellman-ford

9
推荐指数
1
解决办法
1万
查看次数

如何通过Haskell中的弱指针缓存构建一个具有重复消除的无限树

以下代码构建无限树,同时创建所有子树的缓存,以便不创建重复的子树.消除重复子树的基本原理来自对类似国际象棋游戏的状态树的应用:通过改变两个移动的顺序,人们通常可以最终处于相同的游戏状态.随着游戏的进行,无法访问的状态不应继续占用内存.我以为我可以通过使用弱指针来解决这个问题.不幸的是,使用弱指针将我们带入了IO Monad,这似乎已经破坏了足够/所有的懒惰,使得此代码不再终止.

因此我的问题是:是否有可能有效地生成一个没有重复子树的懒惰(游戏状态)树(并且没有泄漏内存)?

{-# LANGUAGE RecursiveDo #-}

import Prelude hiding (lookup)
import Data.Map.Lazy (Map, empty, lookup, insert)
import Data.List (transpose)

import Control.Monad.State.Lazy (StateT(..))
import System.Mem.Weak
import System.Environment

type TreeCache = Map Integer (Weak NTree)

data Tree a = Tree a [Tree a]
type Node = (Integer, [Integer])
type NTree = Tree Node

getNode (Tree a _) = a
getVals = snd . getNode

makeTree :: Integer -> IO NTree
makeTree n = fst <$> runStateT (makeCachedTree n) empty

makeCachedTree :: …
Run Code Online (Sandbox Code Playgroud)

tree haskell functional-programming weak-references infinite

9
推荐指数
1
解决办法
251
查看次数

如何使用Haskell超时功能(在System.Timeout中)停止失控计算?

System.Timeout中的超时功能有时无法停止无限计算。

例如,

timeout 1000 $ print $ length [0..]
Run Code Online (Sandbox Code Playgroud)

Nothing由于超时中断了无限计算,因此按预期返回。但

timeout 1000 $ print $ length $ cycle [1,2,3]
Run Code Online (Sandbox Code Playgroud)

永远循环。

在Mac上,使用ghc或ghci 8.6.4。

我希望第二个示例的行为与第一个示例相同,在1毫秒后中断无限计算并返回Nothing。而是,第二个示例挂起。

haskell timeout infinite infinite-loop

9
推荐指数
1
解决办法
144
查看次数

如何制作永不结束的迭代器?

我只是想知道无限期地迭代一个集合的最简单方法是什么,即当它到达结束时它next();调用第一个对象.我假设这不是Java中已经预定义的函数,所以只是寻找在Java中实现它的最简单方法.

java iterator infinite

8
推荐指数
2
解决办法
6729
查看次数

通过带有角度js的URL传递变量

我正在使用角度来制作电子商务,而我正在设置无限滚动到产品列表页面.一切正常,但我想使用URL来设置页面,因此用户可以通过URL访问特定页面.如何在URL中设置像"pageNumber"这样的变量?比如"www.page.com/page/2/"(我想得到2号并将其传递给商店控制员)

这是我现在的代码

(function() {
var app = angular.module('concurseirosUnidos', ['store-directives', 'ngRoute']);
    app.config(function($routeProvider, $locationProvider){
    $locationProvider.html5Mode(true);
    $routeProvider
    .when('/', {templateUrl: 'partials/products-list.html'})
    .when("/page/$pageNumber"), {
        // probably I'd need to put something here?
    })
     .otherwise({redirectTo:'/'});;
    }
});

  app.controller('StoreController', ['$http', '$scope', function($http, $scope){
    var store = this;
    store.products = [];      

    $http.get('/app/products/products.json').success(function(data){
        store.products = data;
    });

    if(typeof page === 'undefined'){
        var page = 1;   
    }else{
      //if it's defined through the url, page = pageNumberFromURL
    }
    $scope.myLimit = 3 * page;

    $scope.nextPage = function () {
        page++; // I …
Run Code Online (Sandbox Code Playgroud)

infinite infinite-loop infinite-scroll angularjs

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

在没有framedrops的情况下创建无限的cgpath

我需要不断创建一个cgpath.目前我这样做:

 func createLine(){
        var rand = randomBetweenNumbers(1, 2)
        currentY--
        if rand < 1.5{
            currentX--
            CGPathAddLineToPoint(leftPath, nil, currentX, currentY)
        }else{
            currentX++
            CGPathAddLineToPoint(leftPath, nil, currentX, currentY)
        }
        CGPathAddLineToPoint(rightPath, nil, currentX+tileSize, currentY)
        lineNode.path = leftPath
        rightNode.path = rightPath

}
Run Code Online (Sandbox Code Playgroud)

并称之为:

NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("startTile"), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

但问题是,帧随着时间的推移越来越低.有什么我必须改变,以便帧率不再下降?

我的目标是创建一个随机的无尽路径.

frame-rate infinite cgpath sprite-kit swift

8
推荐指数
1
解决办法
545
查看次数

有没有办法分离无限和有限列表?

例如,我正在为列表编写一些函数,我想使用长度函数

foo :: [a] -> Bool
foo xs = length xs == 100
Run Code Online (Sandbox Code Playgroud)

有人能理解这个函数是否可以与无限列表一起使用?

或者我应该总是考虑无限列表并使用这样的东西

foo :: [a] -> Bool
foo xs = length (take 101 xs) == 100
Run Code Online (Sandbox Code Playgroud)

而不是直接使用长度?

如果haskell将具有FiniteList类型,那么长度和foo将是什么

length :: FiniteList a -> Int
foo :: FiniteList a -> Bool
Run Code Online (Sandbox Code Playgroud)

haskell list infinite

8
推荐指数
2
解决办法
254
查看次数

溪流如何停止?

我想知道我何时创建自己的无限流,Stream.generate标准库中的Streams如何停止...

例如,当您有一个包含记录的列表时:

List<Record> records = getListWithRecords();
records.stream().forEach(/* do something */);
Run Code Online (Sandbox Code Playgroud)

流不会无限并且永远运行,但是当遍历列表中的所有项时它将停止.但是这有什么作用呢?相同的功能适用于由Files.lines(path)(源:http://www.mkyong.com/java8/java-8-stream-read-a-file-line-by-line/)创建的流.

第二个问题,如何Stream.generate以相同的方式停止创建流?

infinite java-8 java-stream

8
推荐指数
1
解决办法
2469
查看次数

Haskell-两个列表中的元组列表

我试图实现一个函数(如下所述),它接受两个列表(每个或两个可能是无限的)并返回列表之间所有可能元素对的元组列表

zipInf :: [a] -> [b] -> [(a,b)]
Run Code Online (Sandbox Code Playgroud)

(例如输出应该是这样的,但不一定非常像这样)

zipInf [0 .. 2] ['A' .. 'C'] ~> [(0,'A'),(1,'A'),(0,'B'),(1,'B'),(0,'C'),(2,'A'),(2,'B'),(1,'C'),(2,'C')]

zipInf [] [0 ..] ~> []

zipInf [0 ..] [] ~> []

take 9 (zipInf ['A'] [0 .. ]) ~> [('A',0),('A',1),('A',2),('A',3),('A',4),('A',5),('A',6),('A',7),('A',8)]
Run Code Online (Sandbox Code Playgroud)

我开始像这样实现它:

zipInf :: [a] -> [b] -> [(a,b)]
zipInf [] _ = []
zipInf _ [] = []
zipInf
Run Code Online (Sandbox Code Playgroud)

我想将列表提供给一个帮助函数来生成列表,但是我创建的列表无法编译,也不知道如何处理无限列表

辅助功能 -

oneList :: [a] -> [b] [(a,b)]
oneList [] _ = []
oneList x:xs y:ys = [(x,y)] ++ oneList
Run Code Online (Sandbox Code Playgroud)

haskell tuples list infinite

7
推荐指数
4
解决办法
6848
查看次数