我正在尝试在Java中定义类似于Haskell的仿函数的类.因此,仿函数定义为:
/**
* Programming languages allow only (just simply enough) endofunctor, that are functors from and to the same category.
* In this case, the category is the one of the datatypes (in here Type, in order to make it more clear)
*/
public interface EndoFunctor<X extends Type> extends Type {
/**
* The basic implementation for any element fx
* @param map Transformation function for the type parameter
* @param fx Element of the current class
* @param <Y> …Run Code Online (Sandbox Code Playgroud) 我想定义一个特定的仿函数如下:
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
data ValOrError a b = Val a | Error b
class MF c a b where
mcons :: a -> c -> c
merr :: b -> c
mhead :: c -> ValOrError a b
mtail :: c -> ValOrError c b
Run Code Online (Sandbox Code Playgroud)
我希望我的类型MF上的类型类型c具有类型参数a和b.我试着在这样的数据结构上定义一个过滤函数,如下所示:
mfilter f e =
let h = mhead e in
let t = mtail e in
case h of
Error …Run Code Online (Sandbox Code Playgroud)