import Control.Monad (liftM2)
infixl 4 :+:, :-:
infixl 5 :*:, :/:
data Expr a = Const a
| (Expr a) :+: (Expr a)
| (Expr a) :-: (Expr a)
| (Expr a) :*: (Expr a)
| (Expr a) :/: (Expr a)
deriving (Show, Eq)
evalExpr (Const a) = a
evalExpr (a :+: b) = liftM2 (+) (evalExpr a) (evalExpr b)
evalExpr (a :-: b) = liftM2 (-) (evalExpr a) (evalExpr b)
evalExpr (a :*: b) = liftM2 (*) (evalExpr a) …Run Code Online (Sandbox Code Playgroud) 这是我的第一次尝试:
import java.util.function.*;
import java.util.ArrayList;
public class IO<A> {
private Function<World,Tuple<World,A>> transform;
private class World {
private ArrayList<String> stdin;
private ArrayList<String> stdout;
public World() {
this.stdin = new ArrayList<String>();
this.stdout = new ArrayList<String>();
}
}
private class Tuple<F,S> {
public F fst;
public S snd;
public Tuple(F fst, S snd) {
this.fst = fst;
this.snd = snd;
}
}
public IO(Function<World,Tuple<World,A>> transform) {
this.transform = transform;
}
public IO<A> pure(A a) {
return new IO<A>(r -> new Tuple<World,A>(r,a));
}
public …Run Code Online (Sandbox Code Playgroud) 我试图实现一个简单类型的lambda演算类型检查器.运行完整性测试时,我尝试输入(SKK),我的类型检查器抛出此错误:
TypeMismatch {firstType = t -> t, secondType = t -> t -> t}
违规术语显然是(SKK)
(\x:t -> t -> t.\y:t -> t.\z:t.x z (y z)) (\x:t.\y:t.x) (\\x:t.\y:t.x)
我认为问题源于缺乏多态性,因为当我输入检查这个haskell代码时它工作正常:
k x y = x
s x y z = x z (y z)
test = s k k -- type checks
Run Code Online (Sandbox Code Playgroud)
但如果我专注于这种类型:
k :: () -> () -> ()
k x y = x
s :: (() -> () -> ()) -> (() -> ()) -> () -> ()
s x …Run Code Online (Sandbox Code Playgroud) lambda haskell functional-programming k-combinator s-combinator
我试图模拟e ^ x的泰勒多项式
---Macularian for e^x
e :: (Num a) => a -> Int -> a
e value precsion = sum $ take precsion [((x^n) / product [1..n]) | x <- value, n <- [0..]]
Run Code Online (Sandbox Code Playgroud)
这是我的错误
Main.hs@3:73-3:78不能从e :: Num a => a - > Int - > a的类型签名绑定的上下文(Num a)中推断出(a~ [a]) home/app/isolation-runner-work/projects/75872/session.207/src/Main.hs:2:6-29 a是一个刚性类型变量,由e :: Num a => a的类型签名绑定> Int - > a at /home/app/isolation-runner-work/projects/75872/session.207/src/Main.hs:2:6相关绑定包括value :: a(绑定在/ home/app/isolation -runner-works/projects/75872/session.207/src/Main.hs:3:3)e :: a - > Int - > a(绑定在/ home/app/isolation-runner-work/projects/75872 /session.207/src/Main.hs:3:1)...