117 points · by hamidr · 13y ago · 40 comments
Were I to use Haskell in a real-world project with teammates, I'd have to keep an iron grip on the complexity of the code the team writes. Maintainability (mainly readability/comprehensibility as far as I'm concerned) is far more important for the kind of work I do than theoretical purity and hypothetical applicability to classes of problems the team isn't facing.
I could easily keep things on the rails on my own, but I suppose it's simply unsettling thinking about using Haskell for something sizable; probably due to my own lack of experience with it. I'd love to hear about sizable real-world Haskell projects, since I think FP is the future of software engineering.
Don't take it so seriously.
I think that like Haskell, Scala's flexibility tends to take some people off into the weeds. See, for example, Coda Hale's concerns about using Scala at Yammer, from about 2 years ago:
http://codahale.com/downloads/email-to-donald.txt
However, I think we need to be careful not to overvalue familiarity. Rich Hickey's talk "Simple Made Easy" is well-known, and I think he's correct to tell us that we should value simplicity over ease. I guess the answer is to use functional programming to make our software simpler, and not let cleverness take us off into the weeds.
It seems to me like "off the rails" in a pure language is going to be much nicer than off the rails in a language where mutable state is the norm.
Yes their code sorta worked, but Deity help you when it came time to add a feature or fix a bug.
Contrawise, my next project was with a team of moderately competent Perl programmers. Functions were concise and variable names reasonably clear. Maintenance was a breeze by comparison.
Yes and yes (in my experience).
http://www.joachim-breitner.de/blog/archives/606-Real-World-...
http://corp.galois.com/systems-software
http://www.haskell.org/haskellwiki/Haskell_in_industry
http://cufp.org/conference/schedule/2012
https://www.fpcomplete.com/business/resources/case-studies
_____________
similar for scala http://typesafe.com/company/casestudies
What is the best way to implement it in Haskell since Phi and Psi are real numbers?
Edit: as the user tome told me.... It was about the factorial function not about fibonacci. I blindly see fibonacci.
import Data.Number.CReal
fib :: Integer -> Integer
fib n = round $ (φ^^n - (-φ)^^(-n)) / sqrt 5
where
φ = (1 + sqrt 5) / 2 :: CReal
-- While we at it
fib' n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs) fibs = map fst (iterate (\(cur,next)->(next,cur+next)) (0,1)) fib :: Integer -> Integer
fib x = truncate $ ( 1 / sqrt 5 ) * ( phi ^ x - psi ^ x )
where
phi = ( 1 + sqrt 5 ) / 2
psi = ( 1 - sqrt 5 ) / 2 *Main> 1 / sqrt 5 * (((1 + sqrt 5) / 2) ^ 26 - ((1 - sqrt 5) / 2) ^ 26)
121392.99999999999
happens to be just on the wrong side of the truncate. fac n = product [1..n]
can be replaced with fac n = product [2..n]
and will still do the right thing for n == 0, n == 1. Saves you a multiplication! {-# RULES
"product/1" forall x. product [1..x] = product [2..x]
#-}
fac n = product [1..n]Also, there are implementations in the list that can be implemented in the same way on non functional languages.
I wrote in Ruby a long time ago: https://gist.github.com/jfarmer/82500f2b52c540df5fbc
You still have to implement the exponentiation algorithm, so you're never really going to get O(1). This is better than O(n), though.
With those things implemented it becomes
def fib_phi(n)
((PhiRational.new(0,1)**n - PhiRational.new(1,-1)**n)/PhiRational.new(-1, 2)).a.to_i
end
which is the explicit formula. I'd love to see the same thing in Haskell, which I imagine would look less crazy.You might want to suggest something with the Sterling formula instead, but that would be trickier (it's an approximation, not an exact formula).