前段时间我读到函数类型a -> b对应的关系a ? b,或者是a ? b吗?这对我来说是有道理的,因为如果我们之间有双射(即(a ? b) ? (a -> b, b -> a))两种类型是同构的.同样地,(a = b) ? (a ? b) ? (a ? b).
我知道这不是Curry-Howard-Lambek的对应关系(即类型理论,逻辑和范畴理论之间的对应关系).它是类型理论与其他东西之间的对应关系.我想知道更多关于这种信件的信息.有人能指出我正确的方向吗?
我知道这似乎不是一个编程问题,但它与编程有关,我希望一些函数式程序员能够更多地了解它并指出我正确的方向.
haskell type-theory functional-programming login curry-howard
我在Haskell实施了四个De Morgan定律中的三个:
notAandNotB :: (a -> c, b -> c) -> Either a b -> c
notAandNotB (f, g) (Left x) = f x
notAandNotB (f, g) (Right y) = g y
notAorB :: (Either a b -> c) -> (a -> c, b -> c)
notAorB f = (f . Left, f . Right)
notAorNotB :: Either (a -> c) (b -> c) -> (a, b) -> c
notAorNotB (Left f) (x, y) = f x
notAorNotB (Right g) …Run Code Online (Sandbox Code Playgroud) 考虑以下实现take:
const take = (n, [x, ...xs]) =>
n === 0 || x === undefined ?
[] : [x, ...take(n - 1, xs)];
console.log(take(7, [1, 2, 3, 4, 5])); // [1, 2, 3, 4, 5]
console.log(take(3, [1, 2, 3, 4, 5])); // [1, 2, 3]
console.log(take(1, [undefined, 1])); // []Run Code Online (Sandbox Code Playgroud)
如您所见,它不适用于数组,undefined因为x === undefined这不是测试数组是否为空的最佳方法。下面的代码修复了这个问题:
const take = (n, xs) =>
n === 0 || xs.length === 0 ?
[] : [xs[0], ...take(n - 1, xs.slice(1))]; …Run Code Online (Sandbox Code Playgroud)考虑以下自由幺半群的类型类。
class FreeMonoid f where
inj :: a -> f a
univ :: Monoid m => (a -> m) -> f a -> m
Run Code Online (Sandbox Code Playgroud)
inj向自由幺半群注入一个值,并且univ是自由幺半群的通用属性。
此类的实例应满足以下定律。
univ f . inj = funiv f mempty = memptyuniv f (m <> n) = univ f m <> univ f n请注意,if是thenf的实例,则必须是 的实例。否则,最后两条定律就没有意义。那么,如何指定这个约束呢?这是我尝试过的。FreeMonoid(f a)Monoid
class Monoid (f a) => FreeMonoid f where
inj :: a -> f …Run Code Online (Sandbox Code Playgroud) 我正在深入研究Javascript,并学习构造函数方法的工作原理.
在下面的代码中,我希望我能够覆盖对象的构造函数,以便新创建的实例将使用新的构造函数.但是,我似乎无法使新实例使用新的构造函数.
任何有关正在发生的事情的见解将不胜感激!
function constructorQuestion() {
alert("this is the original constructor");
};
c = new constructorQuestion();
constructorQuestion.constructor = function() { alert("new constructor");}
howComeConstructorHasNotChanged = new constructorQuestion();
Run Code Online (Sandbox Code Playgroud)
使用ghci我计算:
Prelude> let m = [1,2]
Prelude> let ys = [4, 5, 6]
Prelude> m >>= (\x -> ys >>= (\y -> return (x, y)))
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6)]
Run Code Online (Sandbox Code Playgroud)
上面的monadic表达似乎不符合monad关联性法则的任何一方:
(m >>= f) >>= g ? m >>= (\x -> f x >>= g)
Run Code Online (Sandbox Code Playgroud)
我想知道monad关联性如何应用于表达式:
m >>= (\x -> ys >>= (\y -> return (x, y)))
Run Code Online (Sandbox Code Playgroud)
因为return (x,y)关闭周围的函数和包含它的那个函数,似乎在关联性定律的左侧存在的中间monad (m >>= f)在这个例子中不存在.
我喜欢ECMAScript 6允许您编写如下的咖喱函数:
var add = x => y => z => x + y + z;
Run Code Online (Sandbox Code Playgroud)
但是,我讨厌我们需要在咖喱函数的每个参数中加上括号:
add(2)(3)(5);
Run Code Online (Sandbox Code Playgroud)
我希望能够一次将咖喱函数应用于多个参数:
add(2, 3, 5);
Run Code Online (Sandbox Code Playgroud)
我该怎么办?我不在乎性能。
javascript functional-programming currying apply ecmascript-6
的承诺/ A +规范是最小规范之一.因此,实施它是理解它的最佳方式.Forbes Lindesay的以下答案向我们介绍了实现Promises/A +规范,Basic Javascript promise实现尝试的过程.但是,当我测试它时,结果并不令人满意:
? 109 tests passed
? 769 tests failed
Run Code Online (Sandbox Code Playgroud)
很明显,Promises/A +规范并不像看起来那么容易实现.您将如何实现规范并向新手解释您的代码?Forbes Lindesay在解释他的代码方面表现非常出色,但遗憾的是他的实现不正确.
将数字从双精度浮点格式转换为单精度浮点格式会导致精度损失.用于实现此转换的算法是什么?
数字是大于3.4028234e+38还是小于-3.4028234e+38简单地减少到各自的限制?我觉得转换过程比这更复杂,但我找不到它的文档.
floating-point type-conversion double-precision ieee-754 single-precision
var x;
function apply() {
if (x = 1) {
alert("show");
document.getElementById("nav").style.display = "inline";
var x = 2;
} else {
alert("hide");
document.getElementById("nav").style.display = "none";
var x = 1;
}
}
function hide() {
document.getElementById("nav").style.display = "none";
x = 1;
alert(x)
}
Run Code Online (Sandbox Code Playgroud)
我在使用这段代码时遇到了一些麻烦.我使用该功能hide onload并将功能apply链接到按钮单击.
我需要在Haskell中执行以下操作,并且无法想到正确的方法:
for (int i=0; i<100; i++)
for (int a=0; a<100; a++)
foo = (i, a);
Run Code Online (Sandbox Code Playgroud)
我也不希望'重复'返回,所以不是(1,50)和(50,1).关于如何做到这一点的任何想法?
我一直在查看my.class.js的源代码,以了解是什么让它在Firefox上如此之快.这是用于创建类的代码片段:
my.Class = function () {
var len = arguments.length;
var body = arguments[len - 1];
var SuperClass = len > 1 ? arguments[0] : null;
var hasImplementClasses = len > 2;
var Class, SuperClassEmpty;
if (body.constructor === Object) {
Class = function () {};
} else {
Class = body.constructor;
delete body.constructor;
}
if (SuperClass) {
SuperClassEmpty = function() {};
SuperClassEmpty.prototype = SuperClass.prototype;
Class.prototype = new SuperClassEmpty();
Class.prototype.constructor = Class;
Class.Super = SuperClass;
extend(Class, SuperClass, …Run Code Online (Sandbox Code Playgroud) javascript ×6
haskell ×5
curry-howard ×2
ecmascript-6 ×2
apply ×1
closures ×1
constructor ×1
css ×1
currying ×1
duplicates ×1
ieee-754 ×1
if-statement ×1
iteration ×1
jsperf ×1
let ×1
login ×1
loops ×1
monads ×1
object ×1
oop ×1
promise ×1
superclass ×1
type-theory ×1
typeclass ×1