我一直在尝试用JavaScript 编写Sieve of Eratosthenes算法.基本上我只是遵循以下步骤:
这就是我想出的:
function eratosthenes(n){
var array = [];
var tmpArray = []; // for containing unintentionally deleted elements like 2,3,5,7,...
var maxPrimeFactor = 0;
var upperLimit = Math.sqrt(n);
var output = [];
// Eratosthenes algorithm to find all primes under n
// Make an array from 2 to (n - 1)
//used as a base array to delete composite number from
for(var i = 2; i < n; i++){
array.push(i); …Run Code Online (Sandbox Code Playgroud) 我只是想知道是否..真的在 Haskell 代码中的任何地方定义,就像在Prelude 中一样?是enumFromTo一样的吗?我不明白定义是什么?
enumFromTo x y = map toEnum [fromEnum x .. fromEnum y]
[ e1 .. e3 ] = enumFromTo e1 e3
Run Code Online (Sandbox Code Playgroud) 在Python中,我们有del删除变量的语句.
例如:
a = 1
del a
Run Code Online (Sandbox Code Playgroud)
在Lisp中这相当于什么?
(setq foo 1)
;; (del foo) ?
Run Code Online (Sandbox Code Playgroud) 我是Haskell的初学者,并且从"了解你一个Haskell"中学到了一些我对Foldable的Tree实现不了解的东西.
instance F.Foldable Tree where
foldMap f Empty = mempty
foldMap f (Node x l r) = F.foldMap f l `mappend`
f x `mappend`
F.foldMap f r
Run Code Online (Sandbox Code Playgroud)
引用来自:LYOH:"因此,如果我们只为某种类型实现foldMap,我们可以免费获得该类型的foldr和foldl!"
有人可以解释一下吗?我不明白为什么以及如何免费获得foldr和foldl ..
使用多个模式匹配,即使没有点,也不可能有不同数量的参数!
foo True b = b + 2
foo _ = id
Run Code Online (Sandbox Code Playgroud)
不起作用的例子.但
foo True = (+2)
foo _ = id
Run Code Online (Sandbox Code Playgroud)
确实.有时我们只能在函数的一个部分使用无点,所以......
为什么?GHC太难了吗?:'(
这些天我正在学习Scala.我对Haskell略有熟悉,虽然我不能声称对它很了解.
那些不熟悉Haskell的人的括号
我在Haskell中喜欢的一个特点是,不仅功能是一等公民,而且副作用(让我称之为行动)也是如此.执行时将赋予您类型值的操作a属于特定类型IO a.您可以像其他任何值一样传递这些操作,并以有趣的方式组合它们.
事实上,结合副作用是Haskell用它们做某事的唯一方法,因为你无法执行它们.相反,将要执行的程序是由您的main函数返回的组合操作.这是一个巧妙的技巧,允许函数是纯粹的,同时让你的程序实际上做除了消耗功率之外的其他事情.
这种方法的主要优点是编译器知道您执行副作用的代码部分,因此它可以帮助您捕获它们的错误.
实际问题
在Scala中是否有某种方法可以让编译器类型为您检查副作用,以便 - 例如 - 您保证不会在某个函数中执行副作用?
monads functional-programming scala side-effects purely-functional
我试图在一个函数中写3-4 where语句但我得到错误并且无法做到,我试图做类似的事情:
foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= samplefunct1 x
foo2= samplefunct2 x
foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
我知道代码有点无用,但我只是写了这个来举例说明我的意思.
有没有人可以帮助我?提前致谢.
syntax haskell where-clause guard-clause function-definition
我正在寻找haskell中的函数来压缩两个长度可能不同的列表.
我可以找到的所有zip函数只删除比另一个更长的列表的所有值.
例如:在我的练习中,我有两个示例列表.
如果第一个比第二个短,我必须用0填充.否则我必须使用1.
我不允许使用任何递归.我只需要使用更高阶的函数.
我可以使用任何功能吗?
到目前为止,我真的找不到任何解决方案.
Haskell 定义说:
表达式是弱头正常形式(WHNF),如果它是:
- 一个构造函数(最终应用于参数),如True,Just(square 42)或(:) 1
- 一个内置函数应用于太少的参数(可能没有),如(+)2或sqrt.
- 或lambda抽象\ x - >表达式.
为什么内置功能会得到特殊处理?根据lambda演算,部分应用函数和任何其他函数之间没有区别,因为最后我们只有一个参数函数.
haskell lambda-calculus partial-application reduction weak-head-normal-form
haskell ×6
algorithm ×2
arguments ×1
arrays ×1
common-lisp ×1
currying ×1
definition ×1
elisp ×1
enumeration ×1
enums ×1
fold ×1
foldable ×1
guard-clause ×1
javascript ×1
lisp ×1
list ×1
monads ×1
pointfree ×1
primes ×1
reduction ×1
scala ×1
side-effects ×1
syntax ×1
typeclass ×1
where-clause ×1