这是我第一次尝试创建类的自定义实例,如Ord.
我已经定义了一个新的数据结构来表示一个列表:
data List a = Empty | Cons a (List a)
deriving (Show, Eq)
Run Code Online (Sandbox Code Playgroud)
现在我想为List定义一个新的Ord实例,使得List a <= List b意味着"List a中元素的总和小于或等于List b中元素的总和"
首先,是否有必要定义一个新的"sum"函数,因为Prelude中定义的总和不适用于新的List数据类型?那么,我如何为列表定义Ord的新实例?
谢谢
我在Haskell中定义了以下抽象数据类型:
data Trie = Leaf
| Node [(Char, Trie)]
deriving (Eq)
Run Code Online (Sandbox Code Playgroud)
的Node类型是元素的列表(c, t),其中c,是从当前节点到边缘的标签t.
现在我要打印出树的邻接列表.具体来说,我需要每行打印一个边,其中边的格式为:
n1 n2 c
与n1源,n2目标和c边缘的标签.
我可以用我的根节点打印边缘
instance Show Trie where
show = show' 2 1
where show' _ _ Leaf = ""
show' next n1 (Node ts) = unlines $ zipWith (\n2 (c, _) ->
show n1 ++ " " ++ show n2 ++ " " ++ show c)
[next..] ts
Run Code Online (Sandbox Code Playgroud)
但现在我被困在如何以递归方式打印孩子们.特别是,如何为子节点编号?
在包的Control.Proxy教程中pipes-3.1.0,作者提供了这个功能:
cache :: (Proxy p, Ord key) => key -> p key val key val IO r
cache = runIdentityK (loop M.empty) where
loop _map key = case M.lookup key _map of
Nothing -> do
val <- request key
key2 <- respond val
loop (M.insert key val _map) key2
Just val -> do
lift $ putStrLn "Used cache!"
key2 <- respond val
loop _map key2
Run Code Online (Sandbox Code Playgroud)
因为我想要并发应用程序缓存请求,所以我有以下数据类型
newtype Cache k v = Cache (MVar (M.Map k v)) …
假设我有以下特征和扩展该特征的类
trait T { protected def f() }
class C extends T { def f(): println("!") }
object Main extends App {
val c = new C
c.f() // should be a compile error
}
Run Code Online (Sandbox Code Playgroud)
我在 for 的声明中f声明了,以便可以从 的范围内调用它,但不能由其他人调用。换句话说,应该是编译错误。我以为修饰符 from会保留下来,但事实并非如此。protectedTCC.f()protectedT
我可以C.f()像protected在声明中那样重新声明C,但我不想重复自己。在 Scala 中还有其他方法可以做到这一点吗?
我的代码无法编译:
{-# LANGUAGE EmptyDataDecls, GADTs, RankNTypes #-}
import Data.Ratio
data Ellipsoid
data Halfplane
data PointSet a where
Halfplane :: RealFrac a => a -> a -> a -> (a -> a -> Bool) -> a -> PointSet Halfplane
Ellipsoid :: RealFrac a => a -> a -> a -> (a -> a -> Bool) -> a -> PointSet Ellipsoid
type TestFunc = RealFrac a => (a -> a -> a -> Bool)
ellipsoid :: PointSet Ellipsoid -> TestFunc
ellipsoid (Ellipsoid …Run Code Online (Sandbox Code Playgroud) 我有这个代码片段,它使用了大量的GHC扩展:
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import GHC.Exts (Constraint)
data HList :: [*] -> * where
Nil :: HList '[]
Cons :: a -> HList l -> HList (a ': l)
type family All (p :: * -> Constraint) (xs :: HList [*]) :: Constraint where
All p Nil = ()
All p (Cons x xs) = (p x, All p …Run Code Online (Sandbox Code Playgroud) 我有一个界面
public interface Foo<T> {
public void bar(String s, T t);
}
Run Code Online (Sandbox Code Playgroud)
我想写一个方法
public void baz() {
String hi = "Hello";
String bye = "Bye";
Foo<String> foo = new Foo() {
public void bar(String s, String t) {
System.out.println(s);
System.out.println(s);
}
};
foo.bar(hi,bye);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
<anonymous Test$1> is not abstract and does not override abstract method bar(String,Object) in Foo
Foo<String> foo = new Foo() {
Run Code Online (Sandbox Code Playgroud)
我对Java很新,我确信这是一个简单的错误.我怎么写这个?
我有两个类似于filter和的功能takeWhile.
filterAcc, takeWhileAcc :: ([a] -> Bool) -> [a] -> [a]
filterAcc p xs = go xs []
where go [] acc = acc
go (x:xs) acc
| p (x:acc) = go xs (x:acc)
| otherwise = go xs acc
takeWhileAcc p xs = go xs []
where go [] acc = acc
go (x:xs) acc
| p (x:acc) = go xs (x:acc)
| otherwise = acc
Run Code Online (Sandbox Code Playgroud)
他们都采取谓词和列表,以及他们从正规的不同filter,并takeWhile在该谓词采取累积结果作为输入.
我的问题是,虽然filter even [1..] …
我正在Haskell中编写一些数据类型来表示正式的英语语法.
data S = NP VP
Run Code Online (Sandbox Code Playgroud)
到目前为止,句子只是名词短语和动词短语.惊叹于代数数据类型的优雅之美!
我还将一个限定词和形容词定义为:
data D = A | An | The
type Adj = String -- Too many adjectives for me to list, so I make it a type
-- synonym for String.
Run Code Online (Sandbox Code Playgroud)
现在,我遇到了定义NP的问题,这是一个带有可选限定词和形容词的名词.我的第一个天生本能是使用Maybe:
data NP = Maybe D Maybe Adj N
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
Expecting one more argument to `Maybe' In the type `Maybe' In the definition of data
constructor `Maybe' In the data type declaration for `NP'
Run Code Online (Sandbox Code Playgroud)
(请注意,根据我是否导入了Data.Maybe,错误不会改变)
我使用它的唯一方法是使用记录语法:
data NP' = NP' {determiner :: …Run Code Online (Sandbox Code Playgroud)