对于那些有可疑头脑的人来说,这不是家庭作业,只是好奇.
给定一个有限的字母表,是否有可能构建一个由反向词汇顺序的字母表组成的无限长单词列表?
即给出字母表 "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) 所以我遇到了这个美丽的问题,要求你编写一个程序,找出有向图中是否存在负无穷短路径.(也可以认为是在图中存在"负循环").这是问题的链接:
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) 以下代码构建无限树,同时创建所有子树的缓存,以便不创建重复的子树.消除重复子树的基本原理来自对类似国际象棋游戏的状态树的应用:通过改变两个移动的顺序,人们通常可以最终处于相同的游戏状态.随着游戏的进行,无法访问的状态不应继续占用内存.我以为我可以通过使用弱指针来解决这个问题.不幸的是,使用弱指针将我们带入了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
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。而是,第二个示例挂起。
我只是想知道无限期地迭代一个集合的最简单方法是什么,即当它到达结束时它next();调用第一个对象.我假设这不是Java中已经预定义的函数,所以只是寻找在Java中实现它的最简单方法.
我正在使用角度来制作电子商务,而我正在设置无限滚动到产品列表页面.一切正常,但我想使用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) 我需要不断创建一个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)
但问题是,帧随着时间的推移越来越低.有什么我必须改变,以便帧率不再下降?
我的目标是创建一个随机的无尽路径.
例如,我正在为列表编写一些函数,我想使用长度函数
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) 我想知道我何时创建自己的无限流,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以相同的方式停止创建流?
我试图实现一个函数(如下所述),它接受两个列表(每个或两个可能是无限的)并返回列表之间所有可能元素对的元组列表
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) infinite ×10
haskell ×5
list ×2
algorithm ×1
angularjs ×1
bellman-ford ×1
cgpath ×1
dijkstra ×1
frame-rate ×1
iterator ×1
java ×1
java-8 ×1
java-stream ×1
sprite-kit ×1
swift ×1
timeout ×1
tree ×1
tuples ×1