jaberwacky Posted June 6, 2013 Share Posted June 6, 2013 (edited) Hi! I've been diving into functional programming lately. Really neat stuff! So here is my first Haskell program (worthy of being called a program?) fizz :: Int -> Bool fizz x = if x `mod` 3 == 0 then True else False buzz :: Int -> Bool buzz x = if x `mod` 5 == 0 then True else False fizzbuzz = [if x `mod` 15 == 0 then "FizzBuzz" else if fizz x then "Fizz" else if buzz x then "Buzz" else show x | x <- [1..100]] This program is the FizzBuzz program described here: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html Second version with the guidance of a post from that page I linked: fizz :: Int -> Bool fizz x = if mod x 3 == 0 then True else False buzz :: Int -> Bool buzz x = if mod x 5 == 0 then True else False fizzbuzz :: Int -> [Char] fizzbuzz x | mod x 15 == 0 = "FizzBuzz" | fizz x = "Fizz" | buzz x = "Buzz" fizzbuzz x = show x To run: map fizzbuzz [1 .. 100] Edited June 6, 2013 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
monoceres Posted June 6, 2013 Share Posted June 6, 2013 Very nice! I've been thinking of sinking my teeth into some functional languages for a while now after realizing that I was more or less doing functional programming in javascript. The thing I love about functional programming is how expressive it is. Especially with asynchronous callbacks, closures and currying. Haskell is top of the list so I will probably start with it as soon as I finish my current hobby project. The following seems to be the definitive beginners guide to Haskell: http://learnyouahaskell.com/ Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
jaberwacky Posted June 6, 2013 Author Share Posted June 6, 2013 That's where I'm getting my intro to the language. It's excellent but not for someone who has no idea about FP but fortunately that doesn't apply to us. Though it seems you might know considerably more than me. I've been looking monads recently. Making some progress actually. monoceres 1 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
monoceres Posted June 7, 2013 Share Posted June 7, 2013 I don't know that much. The only time I have even written anything in a purely functional language was in a intro course at university where we had to implement Fibonacci number generators with lisp. At that time I didn't even understand recursion very well so the whole thing was lost on me I understand that it's interesting to learn. Having programmed with OOP for many years means you have seen most tricks and the limits of the paradigm seems everywhere suddenly. Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
jaberwacky Posted June 7, 2013 Author Share Posted June 7, 2013 I've read that design patterns are attempts to make up for what a language lacks and that fp doesn't require a lot of design patterns. The problems that design patterns and oop are meant to solve is taken care of already by the nature of fp. Don't quote me on this. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted June 8, 2013 Author Share Posted June 8, 2013 (edited) Ooooohhh sweet relief! I've been at this function for an embarrasingly long time. To average the fibonacci sequence to some number: binet :: (Integral a, Integral b) => b -> a binet 0 = 0 binet 1 = 1 binet n = let sq5 = sqrt 5 phi = (1 + sq5) / 2 tau = (1 - sq5) / 2 in round (1 / sq5 * phi ^^ n - tau ^^ n) Call it like this: binet 1000 or like this: map binet [1 .. 1000] binet 1000 = 43466557686937345383065038351599801447926028001509287862308762971496173677826382369637714275436500023793638849324607123044223660575724291328059660912133884396327066885592759364714095731516706014648330914627584 It looses precision after a point. I guess that's the nature of Binet? Edited June 8, 2013 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jchd Posted June 8, 2013 Share Posted June 8, 2013 Binet (RIP) is certainly not the guilty one here. Launching Mathematica... In[1]=FibBinet[n_Integer] := Floor[N[(((1 + Sqrt[5])/2)^n)/Sqrt[5],2052]] In[2]=FibBinet[10000] - Fibonacci[10000] Out[2]=0 Of course the traditional form of the Binet formula is more prone to precision loss than the equivalent one I used. Even then, the above computation requires at least 2052 digits of internal accuracy to give the correct result for n=10000 and at least 171 digits for n=1000. In your case I suspect that the arbitrary precision library Haskell uses defaults to or is limited to less accuracy. It's funny that I recently experimented how this formula would do with AutoIt, refering to >this thread. Answer was obvious: not so well thanks to limited IEEE representation. Needless to say I'm a big big big fan of Mathematica programming model! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jaberwacky Posted June 10, 2013 Author Share Posted June 10, 2013 I copied the way you did it in Mathematica into Haskell, mostly, and I got the same result as last time. What is that N? Does that determine the amount of accuracy? I guess that Haskell is less accurate with big integers. binet :: (Integral a, Integral b) => b -> a binet 0 = 0 binet 1 = 1 binet n = floor $ (((1.0 + sqrt 5.0) / 2.0) ^^ n) / sqrt 5.0 binet 300 = 222232244629420209822385128170375707838090877146854766455816192 That's a disappointment. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jchd Posted June 10, 2013 Share Posted June 10, 2013 Yes, in MMa, N[<expr>, x] internally works its way thru <expr> to give you at least x decimal digits accuracy. In fact there is a whole set of precision and accuracy parameters than you can tweak to achieve the desired goal with confidence. It's no big surprise that a monster like Mathematica has unmatched features to deal with overly complex or uncommon precision-hungry expressions, at least compared to a generic language where these complexities are not the main features nor the expected highlights. Nonetheless Haskell still has a pretty good arbitrary precision library for what it is and for the tasks you can expect it will do well. A top-notch CAS (computer algebra system) is required to embark an unbelievable set of both low-level and high level functions, optimizations and dedicated features that general-purpose software can't afford to mimic. Have a look for example at the incredible Wolfram Demonstration Project to see that Mma includes not only pure math abilities but also offers high-end graphics, sounds, databases support, distributed computation, ... My personal opinion about the Mathematica language itself is that even if it lacks some features Haskell has built-in (e.g. laziness + filters) it is more versatile and consistent from the ground up: everything being a list makes functional programming more systematic. There are no FP operators acting on tuples of integers only or such. Head and tail of an empty list are indeed emty lists and don't generate exceptions. Some web pages are doing a side by side comparison Mma vs Haskell and decide that Mma is slower on this and that algorithm. It's trivial that an interpreted language can hardly compete in speed with a compiled language. OTOH you can still emit C code with a paid version of Mma for any notebook, then speed is fairly comparable if not faster. Mma also has built-in support for massive GPU computations (both OpenCL and CUDA) which few languages have out of the box. Finally you can still use the power of MathLink to use the computing power of a remote supercomputer (or clusters of such) and have the result in almost real-time in your Mma notebook. I love unique features like up-values and down-values of functions, efficient sparse matrix operations, rules and patterns, built-in debugging and so much more. Mathematica offers all the programming paradigms I can dream of, which I can freely intermix when I need to. I told you I was a big fan! monoceres and jaberwacky 2 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jaberwacky Posted June 10, 2013 Author Share Posted June 10, 2013 (edited) Wow! I'm sold. I'll definitely look into Mathematica. I did not know all of that was available. Edit: Well, I'm not a math type. Would I still benefit? Edited June 10, 2013 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jchd Posted June 10, 2013 Share Posted June 10, 2013 You can have a pretty interesting ride just by browsing the gourgeous live demo site. Then why not look deeper into what the language is with the online help (with use examples). Even if you focus on the language only and let the myriad specialized maths functions apart, I bet you can benefit from the time you're going to spend (not "waste") there. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
jaberwacky Posted June 11, 2013 Author Share Posted June 11, 2013 I looked into the mathematica but I dunno, seems way out of my league. Challenge one from Project Euler. This uses a list comprehension. Dunno if this is the best way to do it. euler1 = sum [x | x <- [1 .. 999], x `mod` 3 == 0 || x `mod` 5 == 0] euler1 = 233168 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jchd Posted June 11, 2013 Share Posted June 11, 2013 (edited) What comes to mind: Total[select[Range[1, 999], (Mod[#, 3] == 0 || Mod[#, 5] == 0) &]] Breakout: Range[1, 999] means the obvious Select[<list>, criterion] is essentially what Haskell calls a filter. Here the criterion is a pure function (denoted by &, meaning lambda function) where # strepresents the first parameter (elements of the list). Total[<list>] is obvious. Got 233168 (unsurprisingly). I see only minor differences in notation. EDIT: I just found a nice solution list for Euler project problems in both Mathematica and F#. Here is a link for your convenience. I didn't look closely, lest run any of them,but I see no reason why the proposed solutions would be off the mark. I don't know either if new features introduced in latest 9+ version could allow more elegant/efficient approaches but this is kind of secondary. Also I'm not trying to evangelise anyone to Wolfram products nor disrepute Haskell or other languages. My opinion is just non-academic and certainly biaised from years of use of an incredibly versatile and powerful language coming with a complete maths/graphics library covering more uses than I shall ever have. Speed number crunching I also do with Pari/GP from H. Cohen, a "must meet" guy guru professor for anyone interested in computational number theory. Edited June 11, 2013 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now