我正在研究UPENN Haskell Homework 6练习5,试图定义一个ruler function
0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,...
其中流中的第n个元素(假设第一个元素对应于n= 1)是power of 2均匀分割的最大元素n.
我想出了一个没有任何可分性测试的构建它的想法:
data Stream x = Cons x (Stream x) deriving (Eq)
streamRepeat x = Cons x (streamRepeat x)
interleaveStreams (Cons x xs) (Cons y ys) =
Cons x (Cons y (interleaveStreams xs ys))
ruler =
interleaveStreams (streamRepeat 0)
(interleaveStreams (streamRepeat 1)
(interleaveStreams (streamRepeat 2)
(interleaveStreams (streamRepeat 3) (...))
Run Code Online (Sandbox Code Playgroud)
其中前20个元素
ruler =
interleaveStreams (streamRepeat 0)
(interleaveStreams (streamRepeat 1)
(interleaveStreams (streamRepeat 2)
(interleaveStreams …Run Code Online (Sandbox Code Playgroud) 在我看来,ssreflect当我们进行依赖模式匹配时,隐式构造函数字段会变成显式字段,并且设置各种隐式选项不会影响此行为.
使用Coq 8.6可以使用以下代码:
Require Import Coq.Unicode.Utf8.
Set Implicit Arguments.
Set Contextual Implicit.
Inductive Vec (A : Type) : nat ? Type :=
nil : Vec A 0
| cons : ? {n}, A ? Vec A n ? Vec A (S n).
Fixpoint append {A n m}(xs : Vec A n)(ys : Vec A m) : Vec A (n + m) :=
match xs with
nil => ys
| cons x xs => cons x (append xs ys) …Run Code Online (Sandbox Code Playgroud) 我知道字节混洗指令,但我想对半字节(4 位值)做同样的事情,具体来说,我想在 64 位字中混洗 16 个半字节。我的洗牌索引也存储为 16 个半字节。最有效的实施是什么?
我正在尝试使用type_traits标头中的is_member_pointer,使用以下语法,例如:
cout << is_member_pointer<decltype(&vector<int>::member)>::value;
Run Code Online (Sandbox Code Playgroud)
在向量的情况下,我得到一个"模板参数无效"和一个罗嗦的"类型...未知"消息,所有迭代器函数和除clear之外的所有修饰函数.其他容器功能也存在类似的故障.我有mingw g ++ 4.6.2.
有没有办法帮助这个?
我试图理解Heinrich Apfelmus 的文章操作Monad教程,他在那里使用GADTs很多.作为一项心理练习,我试图重写他的代码示例,以便他们不再使用GADT.我不能这样做,现在我知道这是因为我的技能有限,还是存在根本问题.
在r/haskell上, Edward Kmett说" 如果你有Rank N类型,你总是可以通过一个类将GADT转换为最终[sic]无标记表示. "
但是,如果我不想使用Rank N类型呢?我想,我必须牺牲一些东西,但我仍然能够做到.
所以,如果我愿意牺牲一些类型的安全性,使用幻像类型,类型类和诸如此类,那么我可以为表达式(有点)编写一个eval函数,如下所示,而不使用LANGUAGE扩展吗?
-- using GADTs :
data Expr = I Int | -- Int -> Expr Int
B Bool | -- Bool -> Expr Bool
Add Expr Expr -- Expr Int -> Expr Int -> Expr Int
Eq Expr Expr -- Expr 1 -> Expr a -> Expr Bool
Run Code Online (Sandbox Code Playgroud) 我有一个简单的memoizer装饰器:
def funcmemo(f):
memo = {}
@wraps(f)
def wrapper(*args):
if args in memo:
return memo[args]
else:
temp = f(*args)
print "memoizing: ", args, temp
memo[args] = temp
return temp
return wrapper
Run Code Online (Sandbox Code Playgroud)
现在,当我通过"@"令牌使用它时,
@funcmemo
def fib(n):
print "fib called with:", n
if n < 2: return n
return fib(n-2) + fib(n-1)
res = fib(3)
print "result:", res
Run Code Online (Sandbox Code Playgroud)
它正常工作,如打印输出中所示:
fib called with: 3
fib called with: 1
memoizing: (1,) 1
fib called with: 2
fib called with: 0
memoizing: (0,) 0
memoizing: …Run Code Online (Sandbox Code Playgroud) 显然,在Haskell中有一种称为无限类型的东西.
例如,当我尝试iterate concatGHCi时,我得到了这个:
*Main> iterate concat
<interactive>:24:9: error:
• Occurs check: cannot construct the infinite type: a ~ [a]
Expected type: [a] -> [a]
Actual type: [[a]] -> [a]
• In the first argument of ‘iterate’, namely ‘concat’
In the expression: iterate concat
In an equation for ‘it’: it = iterate concat
• Relevant bindings include
it :: [a] -> [[a]] (bound at <interactive>:24:1)
Run Code Online (Sandbox Code Playgroud)
我的问题是,究竟什么是无限类型?它们如何适应类型理论以及我可以从哪些资源中了解它们?有没有允许无限类型的编程语言?
Haskell 和函数式编程是我不熟悉的东西,这学期我第一次见到 Haskell。当我尝试进行递归时,我得到了无限循环(不是很自豪)
这是代码
import Data.List
import System.IO
mrLoop :: Int -> Int -> IO()
mrLoop a b = do
if b == 10 then return ()
else
print(a*b)
mrLoop a (b+1)
main = do mrLoop 2 0
Run Code Online (Sandbox Code Playgroud)
所以我试着玩它,不知何故它起作用了
mrLoop :: Int -> Int -> IO()
mrLoop a 10 = return()
mrLoop a b = do
print(a*b)
mrLoop a (b+1)
Run Code Online (Sandbox Code Playgroud)
但是我的一个朋友说这不是 Haskell 的方式。那么我如何以 Haskell 的方式做到这一点呢?为什么第一个代码不起作用?