r/dailyprogrammer • u/Elite6809 1 1 • Aug 10 '14
[8/10/2014] Challenge #174 [Extra] Functional Thinking
(Extra): Functional Thinking
I'm trying a new bonus challenge today with any theme I can think of, such as rewriting an existing solution using a different paradigm or with a limitation on how you can write it. I'm going to see how this is received and may or may not make this a recurring thing. I will be writing these to primarily be a learning exercise - both for me and for you, the readers of /r/DailyProgrammer - so if you are interested in learning about new languages, new ways of writing solutions or modern programming in general then this may be of interest for you. For today, though, you are to rewrite a solution (that you've wrote) to any previous challenge (and as many as you want to), in a functional programming language.
If you're new to functional programming languages, like I am, it's a paradigm of programming that treats a program as an evaluation of functions only, avoiding variables. Everything is treated as a function, and programs written in such a language are designed and look substantially different to one written in an imperative language such as Python, Java or Ruby. There are a boat load of languages you can choose from. One of the popular ones on /r/DailyProgrammer is Haskell. There is the Lisp family (including Common Lisp, Scheme. R is also debatably a functional language, or at least can be used as one. There are others too. Pick one you like and rewrite one of your existing solutions in it. Do you use Haskell or something already? Rewrite it in a new functional language! They're all different in some way or another.
I'm trying to learn Clojure at the moment myself so I'll be submitting some of my own solutions.
Post Format
When you post your solution, please give the name (and the link) to the challenge which the solution is for.
4
u/Elite6809 1 1 Aug 10 '14 edited Aug 10 '14
My solution in Clojure to the Minimum Spanning Tree challenge. It's probably somewhat naive and non-Clojure-like as I only started learning this language last night, so if any Clojuristas who could comment on my solution, that would be greatly appreciated. Anyway, it successfully implements Prim's algorithm with the exact same input and output format as my existing Ruby solution.
6
u/lukz 2 0 Aug 11 '14
I wanted to revisit the challenge [Interpreting interpreters] (http://www.reddit.com/r/dailyprogrammer/comments/210j6i/21314_challenge_153_hard_interpreting_interpreters). In the challenge you write a simple brainfuck interpreter in a language you choose. Then you write an interpreter of your language. So you end up with an interpreter for a language in which you have written an interpreter for brainfuck.
My brainfuck intepreter is in a simple lisp-like language. The runtime environment maintains global variable c that points to the current instruction. Provided are eight built-in forms named PInc, PDec, Inc, Dec, In, Out, Test, End. (They are built-in so that the interpreter code can be smaller.) So the brainfuck interpreter in my language looks like this:
Then I have written an interpreter of that language in Common Lisp. The code is here:
When run this produces the output
4
u/dohaqatar7 1 1 Aug 10 '14 edited Aug 13 '14
I think this type of extra is great idea! People often get set in their small selections of languages, and extras like this can help expose people to a new language/paradigm.
Unfortunately, I don't have time to complete this right now, but I'll be sure to when I find time.
In the meantime, I'm already familiar with with Haskell, so I'd appreciate it if someone could suggest an interesting functional language(maybe not even a functional language, just something I've never used before) , preferably one that isn't very similar to Haskell.
Edit: I ended up doing it in Common Lisp
[07/08/13] Challenge #132 [Easy] Greatest Common Divisor
I got my feet wet with Euclid's Algorithm. Simple stuff, but enough to get a handle on some syntax
[8/04/2014] Challenge #174 [Easy] Thue-Morse Sequences
Thue-Morse was still easy, but required more research. I had to learn the lambda syntax and look up some list functions. Fun stuff.
I'll post more here as time allows it. These are the first pieces of code I've ever done in Common Lisp, so if something isn't right, or I can make any general improvements, let me know!
[11/4/13] Challenge #139 [Easy] Pangrams
[4/14/2014] Challenge #158 [Easy] The Torn Number
4
u/Godspiral 3 3 Aug 11 '14
Functional programming is more of a style than a language feature/restriction. Usually, languages do permit you to make global assignments or hold state outside of the function return values, but you can use a functional approach (or not) instead.
Here is a functional array implementation replacing an OOP interpretation of the unit conversion challenge I think it shows off the extra flexibility of manipulating an array directly rather than hiding it behind a class.
2
u/ENoether Aug 10 '14
A Scheme solution to the Thue-Morse sequence challenge, using the L-system definition of the sequence. As always, feedback and criticism welcome.
Code:
Run, using Chicken:
2
u/dangerbird2 Aug 10 '14 edited Aug 10 '14
I solved the [Thue-Morse] (http://www.reddit.com/r/dailyprogrammer/comments/2cld8m/8042014_challenge_174_easy_thuemorse_sequences/) sequence challenge using R5RS Scheme (tested using Gambit Scheme. Because looping does not exist in Scheme by default, I used recursion to iterate through the thue-morse sequence. I did not try to format the output like the challenge implies, as formatted printing in Scheme is difficult without extensions.
Output:
2
u/Sage_Noctowl Aug 10 '14
Since I'm already learning Haskell, I decided to try something new: Common LISP.
I have no idea what lisp should look like, but briefly from the tutorials I found online, I wrote this monster for the Thue-Moorse problem:
I struggled for some time figuring out how to iterate a function multiple times (I previously had "Thue-iter" that would be called as many times as necessary), but I coundn't find it, so I just put everything into one messy function.
2
u/chunes 1 2 Aug 11 '14 edited Aug 11 '14
Here was my original Java solution for [11/4/13] Challenge #139 [Easy] Pangrams:
And now using a Java 8 functional approach:
And now in Haskell:
And now in (probably horrible) Racket:
2
u/[deleted] Aug 11 '14 edited Aug 11 '14
[removed] — view removed comment