我知道你想在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中添加矩阵的方法
> 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 (+))
为什么地图功能不起作用?
所以这是一个嵌套列表[[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 …
我想了解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 …
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)
基本上,我怎么用Bar后import Foo.Bar,我真的Foo.Bar每次都要打电话吗?
假设这是我在数据库中的内容
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个表中的实体对象,我可以访问Descendant的Ancestor通过Ancestors.First().Descendants.
如果我在祖先的后代(S)或后代的后代(S)递归迭代并打印出其id和name,以下是我的尝试
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#中有以下课程,应该很容易理解
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)
那我在找什么?
这是我试图弄清楚的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)
我努力使国家作为展会的实例,这样我可以看到的动作 …
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 …
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
haskell ×6
c# ×2
constructor ×2
monads ×2
reflection ×2
scala ×2
types ×2
ado.net ×1
enumeration ×1
inheritance ×1
instance ×1
list ×1
loops ×1
map ×1
nested ×1
parameters ×1
state-monad ×1
static ×1
syntax ×1