我正在遵循课程scala课程,所以请注意,您不要只给我已经烘焙的代码,因为这将违反荣誉代码.我不是在寻找代码,而是寻找算法的逻辑,以及如何实现我想到的东西.
首先,问题是:
使用'forall',实现一个函数'exists',它测试一个集是否包含至少一个给定谓词为true的元素.请注意,函数'forall'和'exists'的行为类似于一阶逻辑的通用和存在量词.
几乎从我的理解是:
创建'存在'.确保它在某处实现了.
我创建了forall:
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (contains(s,a)) if(!p(a)) false else iter(a+1)
else iter(a+1)
}
iter(-bound)
}
Run Code Online (Sandbox Code Playgroud)
但是,这个功能如何帮助我以任何方式创建存在?我的意思是我想到的是存在完全相同的功能,但不是在条件满足时迭代,我只需要返回true,因为找到了一个案例.从理论上讲,我不需要关心其他情况,因此只需要在第二个'if'返回true的位置进行迭代(这意味着第一个集合中的一个数字不能应用于第二个函数).让我更好地说明这一点:
def exists(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (contains(s,a)) if(!p(a)) iter(a+1) else true
else iter(a+1)
}
iter(-bound)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是天气,我认为是正确的,如果是,那么我将如何实现forall,因为功能与完成此功能所需的功能完全不同.如果我错过了这一点,你能否指出我应该如何解决这个问题?
注意 请不要给我烘焙代码
我正在尝试构建一个递归函数,如下所示:
private static int partition(int[] array, int low, int high, int pivot_index){
// just irrelevant code
System.out.println("Somehow this point has been reached 1");
if(low < high){
System.out.println("Somehow this point has been reached 2");
//some more code
partition(array,low,high,pivot_index);
}else{
System.out.println("Somehow this point has been reached 3");
//some more code
return high;
}
System.out.println("Somehow this point has been reached 0");
return -1;
}//partition
Run Code Online (Sandbox Code Playgroud)
令我吃惊的是,在运行我的程序并调用此函数后,编译器将打印:
point 1 reached; point 2 reached; point 1 reached; point 3 reached. point 0 reached.
Run Code Online (Sandbox Code Playgroud)
哪个返回-1会导致程序的整个逻辑崩溃.我确信我在这里遗漏了一些东西但是我的程序在 …
我正在阅读有关 JSP 和 Servlet 的头条书,我发现不同的 servlet 有不同的端口,在开发自己的 servlet 时,您应该始终询问哪个端口可用。
现在这可能有一个非常简单的答案,但我从这个 url 运行我的 servlet:
http://localhost:8080/ch1/Serv1
Run Code Online (Sandbox Code Playgroud)
这是否意味着我的 servlet 具有端口 8080?您能给我指出一个可以更详细地解释这些内容的链接吗?
我从 Google 搜索中得到的结果正是书中所说的(我确信这是正确的):所有 servlet 都有不同的端口。
谢谢
我正在尝试按照Joe Armstrong编写的Erlang编程和软件中的第一个示例.这是代码:
-module(afile_server).
-export([start/1,loop/1]).
start(Dir) -> spawn(afile_server,loop,[Dir]).
loop(Dir) ->
receive
{Client, list_dir} ->
Client ! {self(), file:list_dir(Dir)};
{Client, {get_file, File}} ->
Full = filename:join(Dir,File),
Client ! {self(), file:read_file(Full)}
end,
loop(Dir).
Run Code Online (Sandbox Code Playgroud)
然后我在shell中运行它:
c(afile_server).
FileServer = spawn(afile_server, start, ".").
FileServer ! {self(), list_dir}.
receive X -> X end.
Run Code Online (Sandbox Code Playgroud)
在书中,按预期返回文件列表,但是在我的shell中,它看起来好像程序已经冻结.没有任何东西返回但程序仍在运行.我对erlang一点也不熟悉但是我能理解这应该如何工作.
我在Windows 7 64位中运行它.该目录不为空,因为它包含许多其他erlang文件.
我正试图在Golang中读取Stdin,因为我正在尝试为Erlang实现驱动程序.我有以下代码:
package main
import (
"fmt"
"os"
"bufio"
"time"
)
func main() {
go func() {
stdout := bufio.NewWriter(os.Stdin)
p := []byte{121,100,125,'\n'}
stdout.Write(p)
}()
stdin := bufio.NewReader(os.Stdin)
values := make([]byte,4,4)
for{
fmt.Println("b")
if read_exact(stdin) > 0 {
stdin.Read(values)
fmt.Println("a")
give_func_write(values)
}else{
continue
}
}
}
func read_exact(r *bufio.Reader) int {
bits := make([]byte,3,3)
a,_ := r.Read(bits)
if a > 0 {
r.Reset(r)
return 1
}
return -1
}
func give_func_write(a []byte) bool {
fmt.Println("Yahu")
return true
}
Run Code Online (Sandbox Code Playgroud)
然而,似乎give_func_write从未达到过.我尝试在2秒后启动goroutine写入标准输入来测试它. …
由于某种原因,以下代码无法访问.我无法理解为什么我的代码永远不会到达,因为这是一个简单的模式匹配.这里是:
type Occurrences = List[(Char, Int)]
def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match{
case Nil => Nil
case List() => List()
case x => List(x)
case x::xs => combinations(List((x._1,x._2 - 1))) ::: combinations(xs)
}
Run Code Online (Sandbox Code Playgroud)
该算法旨在提取给定列表的所有子列表.
我正在学习Martin Odersky的课程中的scala课程.他给出了一些关于返回类型的精彩例子,有一件事困惑我:
if(true) 1 else false // return AnyVal as this is the closest subtype of both primitive types
Run Code Online (Sandbox Code Playgroud)
我假设以下内容:
if(true) Tweet.comment("hello") else String("Hello") // I assume that this code will return AnyRef
Run Code Online (Sandbox Code Playgroud)
但scala什么时候会返回Any?它会永远归还吗?
std::vector<int> tmp = ...
for(int i = 0; i <= tmp.size() - 3; i+=2){
std::cout << "i: " << i << " , tmp.size(): " << tmp.size() << std::endl;
if(tmp[i] == tmp[i+1]){
final.push_back(tmp[i] * 2);
}else{
final.push_back(tmp[i]);
final.push_back(tmp[i + 1]);
}
std::cout << "\ntmp.size() at the end of loop: " << tmp.size() << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我有以下输出:
为什么循环执行i明显要大得多tmp.size()?
scala ×3
java ×2
recursion ×2
algorithm ×1
c++ ×1
concurrency ×1
erlang ×1
erlang-shell ×1
for-loop ×1
go ×1
hierarchy ×1
inheritance ×1
servlets ×1
subtype ×1