根据pointfree:
\x -> (x, x)
Run Code Online (Sandbox Code Playgroud)
相当于:
join (,)
Run Code Online (Sandbox Code Playgroud)
显示这个的推导是什么?
我是一位Haskell新手,试图把我的头放在函数中的类型绑定以及Haskell如何执行它上。例如,即使fst函数的类型为fst :: (a, b) -> a,编译器也不会抱怨该函数fst'。但是编译器抱怨函数的类型绑定elem'。
fst' :: (a,a) -> a
fst' s = fst s
elem' :: (Eq a, Eq b) => a -> [b] -> Bool
elem' x xs = elem x xs
Run Code Online (Sandbox Code Playgroud) 我对Haskell的where定义范围有疑问.当我有以下函数f,我希望将其传递x给本地定义的函数f1而不明确地将其用作参数时,我得到一个错误,说明类型x与输出中的类型不兼容f1,尽管它应该是相同:
f :: Eq a => a -> [a]
f x = f1 x
where
f1 :: Eq a => a -> [a]
f1 y = [ x, y ]
错误如下:
Couldn't match expected type `a1' against inferred type `a'
`a1' is a rigid type variable bound by
the type signature for `f1' at test.hs:4:11
`a' is a rigid type variable bound by
the type signature for `f' … 您最有可能写下以下哪项?
r = zip xs $ map sqrt xs
Run Code Online (Sandbox Code Playgroud)
要么
r = [(x, sqrt x) | x <- xs]
Run Code Online (Sandbox Code Playgroud)
互联网上的示例代码似乎表明前者更为丰富且是首选方式.
作为我自己的练习,我正在实施Miller-Rabin测试.(通过SICP工作).我理解费马的小定理并且能够成功地实现它.我在米勒 - 拉宾测试中被绊倒的部分是这个"1 mod n"业务.是不是1 mod n(n是一些随机整数)总是1?所以我很困惑"1模数n的非平方根"可能是什么,因为在我看来"1 mod n"在处理整数值时总是1.我错过了什么?
我想知道什么是需要的电话.
虽然我在维基百科搜索并在此处找到它:http://en.wikipedia.org/wiki/Evaluation_strategy,但无法正确理解.如果有人可以用一个例子来解释并指出与按值调用的差异,那将是一个很大的帮助.
evaluation programming-languages evaluation-strategy call-by-value call-by-need
我一直在努力学习生成素数的算法,我在维基百科上遇到了阿特金的Sieve.我理解算法的几乎所有部分,除了少数几个.以下是问题:
以下是维基百科的伪代码供参考:
// arbitrary search limit
limit ? 1000000
// initialize the sieve
for i in [5, limit]: is_prime(i) ? false
// put in candidate primes:
// integers which have an odd number of
// representations by certain quadratic forms
for (x, y) in [1, ?limit] × [1, ?limit]:
n ? 4x²+y²
if (n ? limit) and (n mod 12 = 1 or …Run Code Online (Sandbox Code Playgroud) 我正在写一个小程序,它计算列表中有多少元素不是数字.这是我的代码:
not_number([],0).
not_number([X|T],R):-
not(number(X)),
R1 is R+1,
not_number(T,R1).
not_number([_|Tail],Result):-
not_number(Tail,Result).
Run Code Online (Sandbox Code Playgroud)
如果我执行这样的代码:
?- not_number([1,2,3,5], R).
Run Code Online (Sandbox Code Playgroud)
我得到R = 0(应该是)
R = 0.
Run Code Online (Sandbox Code Playgroud)
但是,如果我在列表中放置一个字符:
?- not_number([1,2,3,5,a], R).
Run Code Online (Sandbox Code Playgroud)
然后我收到这个错误:
ERROR: not_number/2: Arguments are not sufficiently instantiated
Exception: (10) not_number([a], _G247) ?
Run Code Online (Sandbox Code Playgroud)
有人能解释代码有什么问题吗?我是prolog的新手.
来自Project Euler的问题10 是找到给定n下面所有素数的总和.
我只是通过总结Eratosthenes筛子产生的素数来解决它.然后我通过Lucy_Hedgehog(次线性!)找到了更有效的解决方案.
对于n =2⋅10^ 9:
我在Haskell中重新实现了相同的算法,因为我正在学习它:
import Data.List
import Data.Map (Map, (!))
import qualified Data.Map as Map
problem10 :: Integer -> Integer
problem10 n = (sieve (Map.fromList [(i, i * (i + 1) `div` 2 - 1) | i <- vs]) 2 r vs) ! n
where vs = [n `div` i | i <- [1..r]] ++ reverse [1..n …Run Code Online (Sandbox Code Playgroud) 我从真实世界的 Haskell 中读到
它的操作如下:当一个
seq表达式被求值时,它会强制求值它的第一个参数,然后返回它的第二个参数。它实际上对第一个参数没有任何作用:seq仅作为强制评估该值的一种方式存在。
我在那里强调了then因为对我来说它意味着两件事发生的顺序。
从Hackage我读到
seq a b如果a是底部,则的值是底部,否则等于b。换句话说,它将第一个参数评估a为弱头部范式(WHNF)。seq 通常用于通过避免不必要的懒惰来提高性能。关于求值顺序的注意事项:表达式
seq a b不保证a会在 之前求值b。by 给出的唯一保证seq是两者a和b将在seq返回值之前进行评估。特别是,这意味着b可以在 之前进行评估a。[…]
此外,如果我# Source从那里点击链接,页面不存在,所以我看不到seq.
这似乎与此答案下的评论一致:
[…]
seq不能在普通的 Haskell 中定义
另一方面(或在同一方面,真的),另一条评论写道:
“真实”
seq在 GHC.Prim 中定义为seq :: a -> b -> b; …
haskell functional-programming lazy-evaluation order-of-execution weak-head-normal-form
haskell ×6
algorithm ×3
primes ×3
pointfree ×2
types ×2
arguments ×1
call-by-need ×1
clpfd ×1
combinators ×1
evaluation ×1
math ×1
optimization ×1
prolog ×1
scope ×1
sicp ×1
typechecking ×1
typeclass ×1
zip ×1