小编nob*_*ody的帖子

Haskell遍历列表

我知道你想在Haskell中有不同的想法,但有人可以给我一个关于如何迭代列表或嵌套列表并根据列表元素的值打印出一个字符的快速答案.

list1 = [[1 0 0][0 1 0][0 0 1]]
Run Code Online (Sandbox Code Playgroud)

通过迭代这个嵌套列表,它应该打印x为0和y为1

yxx
xyx
xxy
Run Code Online (Sandbox Code Playgroud)

谢谢

haskell loops nested list

4
推荐指数
1
解决办法
2万
查看次数

Haskell矩阵加法/减法

这就是我在Haskell中添加矩阵的方法


    > add :: (Num a) => [[a]] -> [[a]] -> [[a]]
    > add [] [] = [] 
    > add (x:xs) (y:ys) = zipWith (+) x y : add xs ys

加[[1,2],[3,4]] [[5,6],[7,8]]给我[[6,8],[10,12]]

但是,我正在尝试使用一行代替


    > add :: (Num a) => [[a]] -> [[a]] -> [[a]]
    > add = map ((zipWith (+))

为什么地图功能不起作用?

haskell map

4
推荐指数
2
解决办法
2643
查看次数

Haskell中的矩阵构造函数和方法

所以这是一个嵌套列表[[1,2],[3,4]]

我想将它包装在一个名为Matrix的类型中,并使其成为Eq,Num和Show类的实例

我已经为嵌套列表(矩阵)创建了(add,sub,mul)操作.如何重载(+ - *)运算符以便+ map添加, - 映射到sub,以及*映射到mul?所以我可以做到这一点

> ma = Matrix [[1, 2], [3, 4]]
> mb = Matrix [[5, 6], [7, 8]]
> ma + mb
> ma - mb
> ma * mb
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑

这是我到目前为止的尝试

> add = zipWith (zipWith (+))
> sub = zipWith (zipWith (-))
> data Matrix a = Matrix [[a]] deriving (Eq, Show)
> instance Num (Matrix a)
>   where
>   (+) x y = Matrix $ add x y
>   (-) x y = Matrix $ sub x …

constructor haskell types instance

4
推荐指数
2
解决办法
2685
查看次数

正确使用符号

我想了解Monad,我有以下代码

f a b c d =
   do one <- a + b
      two <- c * d
      three <- one + two
      return three
Run Code Online (Sandbox Code Playgroud)

以上编译

但是当我得到一个错误

*Main> f 1 2 3 4

:1:1:
    No instances for (Num (a0 -> t0), Monad ((->) a0), Monad ((->) t0))
      arising from a use of `f'
    Possible fix:
      add instance declarations for
      (Num (a0 -> t0), Monad ((->) a0), Monad ((->) t0))
    In the expression: f 1 2 3 …

monads haskell

4
推荐指数
2
解决办法
575
查看次数

Scala使用最终的静态变量


class Foo(bar: String) {
  import Foo.Bar
  def this() = this(Bar) // this line fails, it seems I can only do
                         // def this() = this(Foo.Bar)  
}

object Foo {
  val Bar = "Hello Bar"
}
Run Code Online (Sandbox Code Playgroud)

基本上,我怎么用Barimport Foo.Bar,我真的Foo.Bar每次都要打电话吗?

syntax static scala

4
推荐指数
2
解决办法
749
查看次数

如何递归迭代实体的属性

假设这是我在数据库中的内容


table Ancestor (  
  idAncestor int not null,  
  name varchar(20) not null,  
)  

table Descendant (  
  idDescendant int not null,  
  name varchar(20) not null,  
  Ancestor_idAncestor int not null  
)  

当ADO.NET生成用于上述2个表中的实体对象,我可以访问DescendantAncestor通过Ancestors.First().Descendants.

如果我在祖先的后代(S)或后代的后代(S)递归迭代并打印出其idname,以下是我的尝试


public void Iterate(Ancestor a)  
{  
   Type t = a.GetType();
   PropertyInfo[] props = t.GetProperties();
   foreach(var prop in props){
      // pseudo code here
      if(prop is entitycollection)
      {
         // how do I convert prop to entity collection here??
         foreach(var p in prop){ …
Run Code Online (Sandbox Code Playgroud)

c# reflection ado.net enumeration

2
推荐指数
1
解决办法
5598
查看次数

通过调用基类的方法打印派生类的方法名称

我在c#中有以下课程,应该很容易理解

public abstract class BaseAbstract
{
    public void PrintMethodNames()
    {   // This line might needs change
        foreach (PropertyInfo pi in typeof(BaseAbstract).GetProperties())
        {
            Console.WriteLine(pi.Name);
        }
    }
}

public class DerivedClass : BaseAbstract
{
    public void MethodA() { }
    public void MethodB() { }
    public void MethodC() { }
}

public class MainClass
{
    public static void Main()
    {
        BaseAbstract ba = new DerivedClass();
        ba.PrintMethodNames();
        // desired printout 
        // MethodA
        // MethodB
        // MethodC
        // but obviously not working
    }
}
Run Code Online (Sandbox Code Playgroud)

那我在找什么?

c# reflection inheritance abstract-class

2
推荐指数
1
解决办法
678
查看次数

"实例显示状态"不编译

这是我试图弄清楚的State Monad代码

data State a = State (Int -> (a, Int)) 
instance Monad State where
    return x = State (\c -> (x, c))     
    State m >>= f = State (\c ->
        case m c of { (a, acount) ->
            case f a of State b -> b acount})

getState = State (\c -> (c, c))
putState count = State (\_ -> ((), count))

instance Show State where  -- doesn't work
    show (State a) = show a   -- doesn't work
Run Code Online (Sandbox Code Playgroud)

我努力使国家作为展会的实例,这样我可以看到的动作 …

monads haskell state-monad

2
推荐指数
2
解决办法
1096
查看次数

为什么这给了我"是一个严格的类型变量绑定"错误

heist :: (Num n) => [n] -> [n] -> n -> n
-- heist [] [] _ = 0
heist w v maxw = func w v i j where
    i = length w
    j = maxw  
func :: (Num n) => [n] -> [n] -> n -> n -> n 
func _ _ 0 0 = 0
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我:

Heist.hs:15:27:
    Could not deduce (n ~ Int)
    from the context (Num n)
      bound by the type signature for
                 heist :: Num …

haskell types

2
推荐指数
2
解决办法
3696
查看次数

在Scala的构造函数上重新分配var参数


object ReassignTest extends App {
  class X(var i : Int)

  def x = new X(10)
  x.i = 20  // this line compiles

  println(x.i)  // this prints out 10 instead of 20, why?
}
Run Code Online (Sandbox Code Playgroud)

那么我将如何为参数创建一个setter i

parameters constructor scala

2
推荐指数
1
解决办法
217
查看次数