Looking for a small project to do as an introduction to functional programming - functional-programming

I've been reading up on functional programming a lot recently, and I finally decided that the best way to understand it is probably just to start using it. I spent some time looking at different reviews of functional languages, and I think I've settled on Haskell because of its supposed elegance and the fact that it seems to be the go-to pure functional language. Most recently I've been coding in Java, Python, and Perl, so I figure for this exercise I might as well pick a language that forces me to only use functional programming ideas rather than something like Scala or Lisp that also supports imperative programming (but if anyone has thoughts or opinions about this, I'd love to hear them).
Anyway, the whole point for learning the ideas of functional programming (for me at least) is that I've always heard that some problems are more naturally solved in that way. And I've always found that it's better to learn new things by applying them somehow rather than just going through mindless tutorials. So, that being said, what are some straightforward problems/projects that I can do to learn the essence of functional programming?

Try working through the Project Euler challenges. They get harder as you go, so tackling them one by one from a functional programming point-of-view would probably be a very good way of learning.

Related

What common task can I try to tackle/solve with various programming languages?

I am looking for a problem to solve which enables me to try out various programming languages and having a means to compare the pros & cons of them. This is similar to ToDoMVC which does the same task with various MVC frameworks.
Lisp 99 Problems looks pretty good but am looking for interesting suggestions that might take on a real world problem and be a touch less mathematical. Failing that, I might tackle a few of these anyway.
Programming languages I'm looking to try this with:
Haskell
Clojure
Javascript
Erlang
Scala
Elm

Which functional language(s) does Clojure share the most in common with?

I don't know much about functional programming but am interested in learning Clojure.
Are there any functional languages that would be a good point of reference to understand how functional programming works in Clojure?
Or is Clojure different enough in its functional programming approach that I would be better off to just focus on Clojure's functional features by themselves?
Clojure is a lisp so learning other lisps will help a lot in getting used to the parts of the "lisp culture" or general way of doing things. Remember that Clojure breaks significantly with Common Lisp though.
Clojure is lazy so learning Haskell will really help get you used to the idea of real lazy programming.
Clojure is concurrent so learning a little bit of Erlang will help though you will need to keep in mind that Erlang includes a lot about distributed programming while clojure is all about concurrent programming that is not necessarily distributed.
Common Lisp of course :) Scheme might be an easier introduction though and easier to get a stable, simple, common environment.
Clojure and Lisp share a lot of ideas with Ruby as well, though the syntax is much different

How do I get my brain moving in "lisp mode?"

My professor told us that we could choose a programming language for our next programming assignment. I've been meaning to try out a functional language, so I figured I'd try out clojure. The problem is that I understand the syntax and understand the basic concepts, but I'm having problems getting everything to "click" in my head. Does anyone have any advice? Or am I maybe picking the wrong language to start functional programming with?
It's a little like riding a bike, it just takes practice. Try solving some problems with it, maybe ProjectEuler and eventually it'll click.
Someone mentioned the book "The Little Schemer" and this is a pretty good read. Although it targets Scheme the actual problems will be worth working through.
Good luck!
Well, for me, I encountered the same problem as you do in the beginning when I started doing OCAML, but the trick is that you must start thinking about what you want from the code and not how to do it!!!
For example, to calculate the square of list's elements, forget about the length of the list and such tricks, just think mathematically like that:
if the list is empty -> I am done
if not, then the list must have a head and tail -> you calculate the square of the head, then ask your function to do the same with the tail.
Just think about the general case and the base one, and that you are emitting data and not modifying it (unless you want to modify it ;) ).
Good luck!
You could check out The Little Schemer.
How about this: http://www.defmacro.org/ramblings/lisp.html
It is a very simple, step-by-step introduction to thinking in lisp from the point of view of a regular imperative programmer (Java, C#, etc.).
For educational purposes I would recommend PLT Scheme. It is a portable and powerful environment with very good examples and an even better documentation. It will help you to discover the thoughts behind functional programming step by step and in a very clean way. Choosing a little application to implement will help you learning the new language.
http://www.plt-scheme.org/
Additionally "Structure and Interpretation of Computer Programs" of H. Abelssn, G. Sussman, and J. Sussman is a very good book regarding Scheme (and programming).
Regards
mue
Take a look at 99 Lispy problems
Some thoughts on Lisps, not specific to Clojure (I'm not a Lisp expert, so I hope they're mostly correct and useful):
Coding in AST
I know little about compiler or interpreter theory, but every time I code in Lisp, it amazes me that it feels like directly building an AST.
That's part of what "code = data" means, coding in Lisp is a lot like filling data structures (nested lists) with AST nodes. Amazing, and it's easy to read too (with the right text editor).
A Programmable Programming Language
So code chunks are just nested lists, and list operations are part of the language. So you can very easily write Lisp code that generates Lisp code (see Lisp macros). This makes Lisp a programmable (in itself!) programming language.
This makes building a DSL or an interpreter in Lisp is very easy (see also meta-circular evaluation).
Never reboot anything
And in most Lisp systems, code (including documentation) can be introspected and hot swapped at run time.
Advanced OOP
Then, most Lisp Systems have some sort of Object System derived from CLOS, which is an advanced (compared to many OOP implementations) and configurable Object System (see The Art of the Metaobject Protocol).
All these features where invented long ago, but I'm not sure they are available in many other programming languages (although most are catching up, e.g. with closures), so you have to "rediscover" and get used to these by practicing (see the books in other answers).
Just remember: it's all data!
Write some simple classic functions that Lisp is good at, like
reverse a list
tell if an atom is somewhere in an s-expression
write EQUAL to tell if 2 s-expressions are equal
write FRINGE to get the list of atoms at the fringe of an s-expression
write SUBST, then write SUBLIS
Symbolic differentiation
Algebraic simplification
write a simple EVAL and/or APPLY
Understand that Lisp is good for these kinds of no-side-effect functional programs.
It is also useful for stateful side-effect (non-functional) programs, but those are more like "programs" than "functions".
Which is better for a given application depends on the application. In general, it should contain no less, and no more, state information than necessary.
Easy!
M-x lisp-mode
OK, OK, so you might not have Emacs for a brain. In all seriousness, what you need to do is to get really good at recursion. This can be quite a brain warp initially when trying to extend the concept of recursion beyond the canonical examples, but ultimately it will result in more fluid, lispy code.
Also, a lot of people get hung up on the parenthesis, and I don't really know why - the syntax is very simple and consistent and can be mastered in minutes. For me, I came to Scheme after having learned C++ and Java, and I always thought that the difference between "functions" and "operators" was a false dichotomy, and it was refreshing to see that distinction eliminated.
As far as functional programming goes, as long as you can wrap your head around the fact that a function is a first-class value and can be passed both into and out of other functions you should be fine. The usefulness of this will become clear over time, but it's enough that you can write function-taking and function-returning functions.
Finally, I'm not sure what support Clojure has for macros, but they're considered an essential part of lisp. However, I wouldn't worry about learning them until you're deeply familiar with the above items - though macros are incredibly useful and versatile, they're also used less often than the other techniques I mentioned.
I'd start with a language that can be interpreted. I found Moscow ML to be fairly easy. It is a lightweight implementation of Standard ML.
My personal practice is to find a small project (something that might take 3-5 nights hacking away) and implement it. How about a blog filter tool? Maybe just a Towers of Hanoi or Linked List implementation (those are usually 1-night projects).
The way it usually works out is I implement it poorly the first time, throw away what I had, and it finally clicks a few hours in.
A HUGE help is taking a course in something like... um... LISP! :) The homework will force you to confront a lot of the concepts and it clicked for me long before the semester ended.
Good luck!!
Good luck. It took me until about halfway through the "Programming Languages" course in college before Scheme "clicked". Once that happened, though, everything just made sense, and I fell in love with functional programming.
Write a Lisp interpreter in Lisp.
If you haven't alrady, read up on what makes lisp a unique language. If you don't do this first, you'll be trying to do the same things you could do in some other programming languages.
Then try to implement some small thing (try to make it useful to you or you might not have the motivation).
Lisp in a box is a great way to get your feet wet.
For me the important thing is to make sure you do everything in a 'lisp-y' way. Don't be tempted to think 'In Java I'd use a for loop here, how do I do for loops in Lisp?' but to go through enough examples and tutorials (as someone pointed out, SICP is perfect for this) that you can start to spot when code looks 'Lisp-y' and recognise common language paradigms.
I certainly know the feeling of looking at some code I've just written and intuitively knowing that it's correctly idiomatic for that language and platform/framework - that, I think, is when it 'clicks'.
Edit: And kudos for choosing a functional language, lesser students would have just done it in Java :)
Who said it is going to click? I'm always confused
But if you think about how much abstraction it is possible to hide away, behind lisp macros. Then your brain will explode.
:)
I'd check out Programming Clojure. It's a great book for non-lispers.
In addition to what other SO'ers have already suggested, here are my 2 cents:
Start learning the language and try out a few simple numerical/hobby problems in the language
IMPORTANT: Post the solution/code to StackOverflow, asking for people's opinion if that is really the LISPy way to do it.
Best of luck!

Which language would you use for the self-study of SICP? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've caught the bug to learn functional programming for real. So my
next self-study project is to work through the Structure and
Interpretation of Computer Programs. Unfortunately, I've never
learned Lisp, as I was not a CS major in college.
While SICP does not emphasize the tools for programming, doing the
exercises entails picking a Lisp-like language to use. It seems like
some implementation of Scheme would be the path of least
resistance. On the other hand, I hear of others who have used Common
Lisp and Clojure. It seems to me that Common Lisp or Clojure would be
more likely to be used in production code, and therefore slightly
better for my resume. BTW, I fully get the argument that learning a
language is worthwhile for its own sake, but learning a language that
helps my resume is still a benefit. I'm a capitalist and an academic
about my learning.
If you had to self-study SICP, which language would you pick and why?
Ideally, I would like to use a language that can run on the JVM.
I can certainly work with a language where REPL works with bash
and emacs.
ADDITION: have any of you tried reading SICP without using Scheme? If so, what was your experience like?
Use Scheme. It is one of the simplest and easiest languages in existence, and you will spend very little time learning enough of it to understand SICP. Once you understand SICP, you will see how the concepts apply in any language.
Use DrScheme. As others have said, Scheme is a simple language, and DrScheme is a great environment to use it in which has a lot of support and mediocre-to-good documentation.
Not a direct answer but I expect this information to be useful for anyone working through SICP. Be sure to have a look at the videos here:
http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/
There are 20 episodes of an hour each. They were presented by Abelson and Sussman in 1986 for Hewlett Packard employees. I put them on my iPod and watched them while commuting. Fascinating.
Also, the full text of the book is available online at http://mitpress.mit.edu/sicp/
As someone who hires people, I'll tell you that having Scheme on a resume is a good thing. Having Scheme, SML, Ocaml or Haskell on your resume suggests you are a very well rounded programmer, and quite a thinker.
That said, if you are trying for functional programming, why not Haskell instead? Scheme is multiparadigm, it can be OO, Funcitonal, Streams based, or anything else under the sun. This makes it awesome to try out new programming styles and paradigms, but if your goal is strictly functional, it can be a problem. (You will end up writing non functional code and not realizing it.)
I agree that you should just use Scheme. However, if you really have the itch to use Common Lisp or Clojure, I'd pick the latter. Scheme and Clojure are both Lisp-1s, so the code in the book will be more congruent between the two (except for tail calls, but if you understand how to compensate you'll be fine). Common Lisp is a Lisp-2 and will probably obscure the beauty of what SICP is trying to teach you.
The code in the book is Scheme so you'll have to read it anyways - you might as well write it. You might even like it!
To get real value out of the book you'll have to use Scheme. Which implementation of scheme depends on your current environment:
Windows - Dr Scheme (PLT Scheme) - http://download.plt-scheme.org/
Linux - If this is a remote account - you may consider MZScheme (PLTScheme) (http://download.plt-scheme.org/) otherwise you'll want to use Dr Scheme if this is a local instance of Linux.
I think Clojure fits what you want to do just perfectly. It's much more functional than Scheme because the data structures are immutable and it can be very useful as it runs on the JVM. But, be aware that you'll end up learning Scheme anyway to be able to understand the code in the book.
I've caught the bug to learn functional programming for real.
From what I've heard, SICP is about a lot more than just functional programming.
Caveat: I have not read the whole book
Since the examples rely on closures and continuations, you would be better served by using a language with both of those features, otherwise you would need to implement them yourself.
For example, writing a metacircular evaluator in Scheme leverages the fact that Scheme provides closures and continuations.
I used lua when I had a look at sicp
works out pretty well
Use anything but scheme.
While using something else then scheme, you will be encouraged to think more, and avoid temptation to just retype the examples. It is a good thing.
Of course, it has to be similar enough, in lisp-1 sense, so clojure and arc are good to go.
I have used scheme for my self-study. The best way to learn from SICP is to do all the exercises relegiously.
I have used Gnu guile for scheme.
While you could use something other than Scheme, you'd be needlessly adding extra work and possibly cutting yourself off from fully understanding what the book is about. SICP was an introductory programming book. It is a stepping stone to deeper topics in computer science. Getting bogged down in 'translating' from Scheme to CL or Clojure would probably obscure the finer points. That would be a shame, because SICP is truly a gateway to understanding what programming is really about.
Learning Scheme is really straight forward (especially compared to both CL and Clojure) and, in fact, the introductory course as well as the book, assumes the student doesn't know it already. CL and Clojure carry considerable baggage relative to the task at hand.
I hear of others who have used Common Lisp and Clojure.
You should use whatever language most motivates you, but 99% of folks working through SICP are going to use Scheme.
I worked through (most) of it earlier this year, and used Common Lisp, simply because I didn't have Scheme available (don't ask).
As has already been noted, Scheme is a Lisp-1 language whereas Common Lisp is a Lisp-2. There are enough differences between the languages to mean that you have think carefully about translating the code in the book, so it forced me to really get to grips with the material.
but learning a language that helps my resume is still a benefit.
You should try using VB6 or COBOL, then, as there is a lot of billing work out there for it.
I think Scheme would be the natural choice (since it is the "native" language of SICP)
However, since the true value of SICP comes from the concepts rather than the mechanics of the particular language, I think it would be a valuable learning exercise to attempt it in any Lisp-like language. I've personally tried some of the exercises in Clojure and they all translate pretty well.
For those interested there is an ongoing project to create a Clojure translation of SICP.

What's a good beginning text on functional programming? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I like to study languages outside my comfort zone, but I've had a hard time finding a place to start for functional languages. I heard a lot of good things about Structure and Interpretations of Computer Programs, but when I tried to read through it a couple of years ago it just seemed to whiz over my head. I do way better with books than web sites, but when I visit the local book store the books on LISP look kind of scary.
So what's a good starting point? My goal is to be able to use a functional programming language to solve simple problems in 6 months or so, and the ability to move to more advanced topics, recognize when a functional language is the right tool for the job, and use the language to solve more problems over the course of 2-3 years. I like books that are heavy on examples but also include challenges to work through. Does such a thing exist for functional languages?
The Little Schemer teaches recursion really well, and it's fun and simple to read.
I also liked The Scheme Programming Language for a broader introduction into the language.
Try Real World Haskell. It's free online.
SICP is a great book.
This is probably my bias, but I thought ocaml was pretty easy to get into. You have the option of programming in a few different styles until you're completely comfortable. I posted a bunch of links to Haskell and Ocaml references that are books, with examples et cetera that seem right up your alley.
If you prefer Lisp, you can try to power through the 99-problems in Lisp(which you can do in any language, really), or you can watch the lectures from the people who wrote SICP.
Further down the road, get a hold of "Purely Functional Data Structures", as it'll get into the hard-core deep design and considerations you have to take into account in functional languages --it uses ML (which ocaml derived from).
I really recommend "On Lisp" from Paul Graham.
It is concise and very readable even for beginners in functional programming (as I was when I read it). It contains a lot of very short examples, each which helps to understand one single thing.
I often thought reading this book: this is just the language containing exactly the features I ever wanted in other (nonfunctional) languages, but never got. :-(
And this is exactly the book to learn it, always comprehensible, sometimes even funny!
You may get it for free at the author's site!
I really like Thompson’s “Haskell: The Craft of Functional Programming” because it’s well written and Haskell allows an easier start than other functional languages while being completely pure (unlike Lisp or Scheme).
Since there are a bunch of different functional programming languages, it's hard to recommend books. But if you're interested in Common Lisp, recently I've been reading "Practical Common Lisp" by Peter Seibel, which you can check out online for free before dropping your hard earned cash on it. It's a pretty gentle introduction to CL, with great explanations and tons of examples. Seibel's a great writer (example: read the story of Mac,) he's good at keeping you engaged, which is really where SICP falls down, I think. It's just so dry! But while Practical Common Lisp is pretty example-heavy, it doesn't really have challenges to work through, although the examples are mostly designed to let you continue to work and build on them.
Another good book, this one Scheme-oriented: How to Design Programs. (Online)
I haven't had as much time with this book, being more of a Lisper than a Schemer myself, but it's well written, has good explanations and examples, and has lots of exercises to work on. It seems pretty popular in the Scheme crowd.
The Schemers Guide and related software - seriously good stuff
http://www.schemers.com/tsg.html
Check out Introduction to functional programming. It offers a different perspective.
I found The Little Schemer a great, great introduction to functional programming. It's entirely based on simple, bite sized examples which are built up upon as the book goes on.
I learned from Jeffrey Ullman's Elements of ML Programming, which is pretty good. It loses points for being about Standard ML, when OCaml, F#, and Haskell are (seemingly) more popular.
I feel Purely Functional Data Structures by Chris Okasaki is worth a look.
FYI http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
Haskell is a very good functional programming language for beginners. Someone had asked about good resources for Haskell, so I will point you there.
If you are looking for a good book on Functional Programming, I would recommend "Functional Programming: Practice and Theory" by Bruce J. Maclennan.
It is however required that you brush up on your Set Theory and Logic before giving it a read. It includes examples in LISP, Haskell and other languages.
If you have experience with .NET, Expert #F is good.
F# is derived from OCaml. Lisp is more pure as functional languages go.
Real-World Functional Programming (with examples in F# and C#)
I have heard good things about Haskell Functional Programming, but I also found this list of functional programming books at amazon that might be helpful to you.

Resources