我试图在Haskell中表达一个L系统https://en.m.wikipedia.org/wiki/L-system,特别是Lindenmayer的原始L系统,用于模拟藻类的生长.
变量:AB
常数:无
公理:
规则:(A→AB),(B→A)
对我来说,解决这个问题的自然方法是将规则应用于列表中的每个元素,这对我来说意味着我可以使用某种类型的字符串替换来对解决方案进行建模.
对于"字符"列表[A,B,A我们应用规则并获得[A→AB,B→A,A→AB] = [A,B,A,A,B](对于此模型为了与Haskell很好地配合,你必须将AB视为列表[A,B],我们将与上述规则产生的任何其他结果相结合.
我已经生成了下面的代码,其中包含数据构造函数,不必处理除A或B之外的其他字符,
data Letter = A | B deriving (Show, Eq)
type Alphabet = [Letter]
algae :: Alphabet -> Alphabet
algae = concat . map (\c -> if
| c == A -> A:[B]
| c == B -> [A])
Run Code Online (Sandbox Code Playgroud)
上面的代码是这样的,用自己作为参数调用它会产生预期的结果,即.那
algae $ algae $algae [A] = [A, B, A, A, B]
Run Code Online (Sandbox Code Playgroud)
重复的应用程序按预期工作.
我接下来要完成的是将函数递归地应用到自身,但未能表达这一点.通过这个我的意思是我希望能够调用该函数,无论是algae [A]或者只是algae(这需要更改类型签名algae :: Alphabet),这会产生一个无限的列表,通过将藻类无限次地应用到自身上来获得这个列表.
由于我已经承认失败,我已经查看了http://hackage.haskell.org/package/lindenmayer-0.1.0.0/docs/Lindenmayer-D0L.html但是我无法理解代码,因为它(还)还找到了其他代码同样令人困惑的实施.
我已尽力尝试使用使用folds和 …
是否有可能在Kotlin中标记"不关心"?我无法找到合适的语法,Haskell和Python使用_符号,但这在Kotlin中不起作用.
例,
data class DesugarExample(val foo: Int, val bar: Int, val baz: Int)
fun desugarIt() {
val (useFoo, dontCare, useBaz) = DesugarExample(1, 2, 3)
}
Run Code Online (Sandbox Code Playgroud) 什么是用于提供设计原理iter_mut的功能HashMap,但不是HashSet在锈?
卷一个人自己是否是一个失礼(假设甚至可以做到)?
拥有一个可以缓解导致的情况
以前的借款
X发生在这里; 在X借入结束之前,不可变借款会阻止后续移动或可变借款
一个非常复杂的例子(Gist),它没有显示参数传递的原因.有一个简短的评论解释痛点:
use std::collections::HashSet;
fn derp(v: i32, unprocessed: &mut HashSet<i32>) {
if unprocessed.contains(&v) {
// Pretend that v has been processed
unprocessed.remove(&v);
}
}
fn herp(v: i32) {
let mut unprocessed: HashSet<i32> = HashSet::new();
unprocessed.insert(v);
// I need to iterate over the unprocessed values
while let Some(u) = unprocessed.iter().next() {
// And them pass them mutably to another function
// as I will process …Run Code Online (Sandbox Code Playgroud) 如何识别进程是否是使用其进程的另一进程的子/孙pid?
我想能够定义一个自定义数据类型,而不是使用类型别名来确保传递正确的值,下面是一个可能看起来如何的草图,
module Example (fromList) where
import Data.Ord (comparing, Down(..))
import Data.List (sort)
data DictEntry = DictEntry (String, Integer) deriving (Show, Eq)
instance Ord DictEntry where
(DictEntry (word1, freq1)) `compare` (DictEntry (word2, freq2))
| freq1 == freq2 = word1 `compare` word2
| otherwise = comparing Down freq1 freq2
data Dictionary = Dictionary [DictEntry] deriving (Show)
fromList :: [(String, Integer)] -> Dictionary
fromList l = Dictionary $ sort $ map DictEntry l
Run Code Online (Sandbox Code Playgroud)
但是,我还想保留底层类型的"list-ness",而不必打开和重新包装[DictEntry],也不必定义实用程序函数,如head :: Dictionary -> DictEntry和tail …