|
|
Subscribe / Log in / New account

looks like Haskell

looks like Haskell

Posted Apr 18, 2013 2:46 UTC (Thu) by rsidd (subscriber, #2582)
In reply to: looks like Haskell by droundy
Parent article: A taste of Rust

There are also important algorithms (eg dynamic programming, any kind of linear algebra) that are ill-suited to a purely functional language like Haskell. It can be done but hoops need to be jumped through and the results are (to me) unreadable. Sometimes it is clearer and more efficient to have a mutable array or matrix where operations are done in-place. And recursion instead of looping is a nice idea until you try to make it tail-recursive for efficiency and find it is ugly or just impossible. I prefer the approach of OCaml and (apparently) Rust where one is allowed to declare variables mutable, write for-loops, etc.


to post comments

looks like Haskell

Posted Apr 19, 2013 9:24 UTC (Fri) by peter-b (subscriber, #66996) [Link] (4 responses)

Given that every loop has an isomorphic tail recursive form, I suspect that you are running up against "ugly" much more often than "impossible"!

looks like Haskell

Posted Apr 19, 2013 9:39 UTC (Fri) by rsidd (subscriber, #2582) [Link] (3 responses)

Read "impossible" as "impossible for me to get my mind around" :) I'm thinking of nested loops in particular. Also things like the Fibonacci function where a naive implementation

fib 0 = 0
fib 1 = 0
fib n = fib (n-1) + fib (n-2)

is not just non-tail-recursive but incredibly inefficient. And every "functional" efficient version that I've seen is just too hard to understand (for me) by reading the code. And that's for a trivial beginner-level example.

looks like Haskell

Posted Apr 19, 2013 9:54 UTC (Fri) by neilbrown (subscriber, #359) [Link] (2 responses)

How about something like:

fib 0 = (1,1)
fib n = (a+b, a) where (a,b) = fib (n-1)

That's reasonably efficient - not sure how easy it is to make it tail recursive.

looks like Haskell

Posted Apr 19, 2013 10:19 UTC (Fri) by rsidd (subscriber, #2582) [Link]

Yes, thanks, that's efficient though I'm not sure how easy it is to make it tail-recursive either. When it's more complicated stuff like matrix multiplication or Needleman-Wunsch, it's just too hard for me -- and, I suspect, for most "normal" people. Haskell has clearly proved that it is very useful for some people and some tasks, but I suspect that's the maximum for pure functional programming.

looks like Haskell

Posted Apr 21, 2013 15:12 UTC (Sun) by ballombe (subscriber, #9523) [Link]

Converting your code to tail-recursion give:

fib n = fibr n 1 1 where
fibr 0 a b = (a,b)
fibr n a b = fibr (n-1) (a+b) a

(thought there are much faster algorithms to compute this)

looks like Haskell

Posted Apr 19, 2013 17:38 UTC (Fri) by b7j0c (guest, #27559) [Link]

graphs have also been a problem for pure functional languages


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds