鉴于像haskell,erlang这样的语言得到了如此多的赞誉,为什么它们都不能成为主流语言?
是因为他们的学习曲线吗?还是太多的符号表示法?
oop erlang haskell programming-languages functional-programming
我有一个类似于此的函数A,它将函数B应用于目录中的每个文件.每个文件都有一定数量的"条目"; 函数B将当前的条目总数作为参数,并返回当前文件中找到的新条目数.
此外,我需要计算处理的文件数,并在每次处理文件时显示此计数.由于我的命令背景,我提出了2个可变变量和一个for循环.
let files = Directory.EnumerateFiles sourceDirectory
let mutable numEntries = 0
let mutable numFiles = Seq.length files
let mutable index = 0
for file in files do
printfn "done %d of %d" index numFiles
let numNewEntries = processFile file numEntries
numEntries <- numEntries + numNewEntries
index <- index + 1
Run Code Online (Sandbox Code Playgroud)
那么,有几个问题:
我正在努力将我的服务器端编程语言从PHP修改为Haskell,因此我并不完全熟悉函数式语言设计.
在这里,我想将以下php函数的部分处理代码转换为Haskell:
function loop($A) {
$array1 = array();
$num = (int)$A;
if ($num == 0)
$array1 = "0";
for (int $i=0; $i<$num; $i++)
foreach (loop($i-$num) as &$value)
$array1[] = "+ ".$value." "; //concat
return $array1;
}
Run Code Online (Sandbox Code Playgroud)
我想会有大量使用map/mapM_函数,但积累如何工作?我开始认为增加的安全性在这一点上不值得改造.
谢谢.
当我在monads上阅读本教程时,找到了以下表达式.
数据M a =提高异常| 回来一个
type Exception = String
它表示a在Raise Exception和Return a中用作类型变量和一系列值,但我不明白M的使用(或含义).如果M是数据类型为什么它像M一样被使用?
我试图做一个方法,它检查两个原子的值,如果它们相互之间它会说它是假的,反之亦然,如果它们不相同它就会是真的.
我的代码看起来像这样:
b_not(X, Y) ->
if
X=:=Y ->
false;
else
true;
end.
Run Code Online (Sandbox Code Playgroud)
这里我的方法的名称是"b_not",它代表boolean_not.如果X完全等于Y,我会用if语句检查它会说假.好吧,我可以做的那部分.但是当我尝试制作else语句时,问题出现了.我似乎没有得到这一部分,我试图在互联网上搜索,但我似乎没有得到它.所以如果你能告诉我if-else语句在Erlang中是如何工作的,我会很高兴的!
谢谢阿列克谢!
我有一个问题:我在scala中有一个列表(List("Entry1","Entry2","Entry3")),我想打印此列表功能.所以我不想使用任何类型的循环.
我知道可以使用某种代码:
def test(input:List[_])
input match
{
case //and what now?
case _ => //recursion I guess?
}
Run Code Online (Sandbox Code Playgroud)
我想使用此方法打印此列表,新行上的每个元素.
有人可以帮帮我吗?
谢谢!
我是Scala的新手,想知道这是否是正确的写作方式:
def createCol(prior: List[Int], current: List[Int]): List[Int] = {
if (prior.isEmpty) List(1) ++ current
else if (prior.tail.isEmpty) // begin of the block to improve
createCol(prior.tail, current ++ List(prior.head))
else
createCol(prior.tail, current ++ List(prior.head + prior.tail.head))
}
Run Code Online (Sandbox Code Playgroud)
我感兴趣的部分是这样的:
if (prior.tail.isEmpty)
createCol(prior.tail, current ++ List(prior.head))
else
createCol(prior.tail, current ++ List(prior.head + prior.tail.head))
Run Code Online (Sandbox Code Playgroud)
因为我正在重复几乎相同的函数调用,createCol所以我尝试了这个:
val ssum = if (prior.tail.isEmpty) prior.head else prior.head + prior.tail.head
createCol(prior.tail, current ++ List(ssum))
Run Code Online (Sandbox Code Playgroud)
这样做的最佳或推荐方法是什么?
谢谢
我正在玩JS,并有以下代码片段
var Dog = function(name) {
this.name = name
}
Dog.prototype= {
'bark': function() {
alert(this.name + ' is barking');
},
'run': function() {
alert(this.name + ' is running');
}
}
var dogs = [new Dog('first'), new Dog('second'), new Dog('third')];
function invokeOnDog(what) {
if(what === 'bark') {
for(var i=0; i<dogs.length; i++) {
dogs[i].bark();
}
}
if(what === 'run') {
for(var i=0; i<dogs.length; i++) {
dogs[i].run();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是简化这个invokeOnDog功能,因为它重复两次相同的模板.我正在考虑以某种方式返回应该在对象上调用的方法,但不知道如何做到这一点.
你能帮帮我吗?
编辑:
感谢您的快速回复.如果"what"与调用方法具有相同的名称,则它们都可以.但如果这两者之间没有匹配怎么办?
invokeOnDog('aggresive')应该调用bark方法并invokeOnDog('scared')应该调用run
我想创建一个名为Structured的结构数据类型,可用于表示String,Int和List.例如,结构类似于:[ Int,String,Int,[ Int ]].
问题1:如何创建此数据类型?
data Structured = ...
Run Code Online (Sandbox Code Playgroud)
问题2:一个名为Confirm的函数确认输入满足限制,并且签名类型为confirm :: Restriction - > Structure - > Maybe Bool
我正在读某个地方的咖喱功能,听起来很混乱.这个例子让我更加困惑.可以说我有一个功能:
power :: (Int, Float) -> Float -- computes the nth power of b
power (n, b) =
if n == 0 then 1.0 else b * power (n-1, b)
Run Code Online (Sandbox Code Playgroud)
现在,我定义另一个函数powerc:: Int -> Float -> Float,使得
powerc n b =
if n == 0 then 1.0 else b * powerc (n-1) b
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下这个功能是如何powerc发布的power.
haskell ×5
erlang ×2
scala ×2
accumulator ×1
f# ×1
if-statement ×1
javascript ×1
list ×1
loops ×1
monads ×1
oop ×1
printing ×1
recursion ×1
refactoring ×1