1
Ich versuche LCM in einer Zeile in Scala zu implementieren. DieseFP LCM in Scala in 1 Zeile
ist, wie ich es mit 2 Funktionen implementiert haben:
def gcd(a: BigInt, b: BigInt):BigInt=if (b==0) a.abs else gcd(b, a%b)
def lcm(list: Seq[BigInt]):BigInt=list.foldLeft(BigInt(1))((a, b) => (a/gcd(a,b))*b)
Wie würden Sie gcd in einen Lambda innerhalb LCM konvertieren?
ohne irgendeine Art von Festpunkt-Operator (das glaube ich nicht existiere bereits in den Bibliotheken Scala - man kann definiere es, aber das ist eine zusätzliche Zeile), ich glaube nicht, dass du das tun kannst. Das Problem ist, dass "GCD" anonym, aber auch rekursiv sein muss. – Alec
@Alec Ich bin gespannt auf eine Antwort mit Fixpunkten – soote