Hot answers tagged

47

For the background you gave, if I may paraphrase: You're familiar with Ruby/Python. You don't see the advantages of Clojure yet. You don't find either Lisp or Clojure syntax clear. ...I think the best answer is to read the book Clojure Programming by Emerick, Carper and Grand. The book has numerous explicit code comparisons with Python, Ruby, and Java, ...


45

The closest equivalent to looping over an array in most functional languages is a fold function, i.e. a function that calls a user-specified function for each value of the array, passing an accumulated value along the chain. In many functional languages, fold is augmented by a variety of additional functions that provide extra features, including the option ...


41

Well, BASIC had LET for assignment as part of the syntax from the start in 1964, so that would predate the use of let in Lisp, which as Chris Jester-Young points out didn't appear until the 1970s according to Evolution of Lisp. I don't believe COBOL, Fortran, or ALGOL have LET in their syntax either. So I'm going to go with BASIC.


40

Names/documentation should tell you what you are doing. Implementation should tell you how you are doing it. Comments should tell you why you do it the way you do.


34

In theory, loose function-data coupling makes it easier to add more functions to work on the same data. The down side is it makes it more difficult to change the data structure itself, which is why in practice, well-designed functional code and well-designed OOP code have very similar levels of coupling. Take a directed acyclic graph (DAG) as an example ...


33

You could easily convert it to recursion. And it has nice tail-optimized recursive call. Pseudocode : public int doSomeCalc(int[] array) { return doSomeCalcInner(array, 0); } public int doSomeCalcInner(int[] array, int answer) { if (array is empty) return answer; // not sure how to efficiently implement head/tails array split in clojure ...


32

Haven't watched the full Rich Hickey presentation, but if I understand him correctly, and judging from what he says about the 29-minute mark, he seems to be arguing about types killing reuse. He is using the term "interface" loosely as a synonym for "named type", which makes sense. If you have two entities { "name":"John" } of type Person, and { "name": "...


30

I'd like to add a theoretical point of view: In classical lambda calculi, let is just syntactic sugar. For example let x = N in M can be rewritten simply as (λx.M)N So its first appearance in early (functional) languages isn't that interesting. However, it become very important with the invention of Hindley-Milner type system and its type inference ...


28

Yes, it is entirely realistic - not many people seem to be doing it yet but I think that is only a matter of time (Clojure is pretty new after all!) I've personally written an open-source game in Clojure which runs as a Swing application (https://github.com/mikera/ironclad) so have some experiences to share which may be useful. On average you probably want ...


28

He is likely referring to the basic fact that an interface can not be instantiated. You can not reuse an interface. You can only implement code that supports it, and when you write code for an interface there is no reuse. Java has a history of providing frameworks of many API(s) that take an interface as arguments, but the team who developed the API never ...


23

Naming conventions stay lowercase for functions use - for hyphenation (what would be underscore or camel case in other languages). (defn add-one [i] (inc i)) Predicates (i.e. functions returning true or false) end with ? Examples: odd? even? nil? empty? State changing procedures end in !. You remember set! right? or swap! Choose short variable ...


22

I feel your pain, I would love to do more coding in functional programming (Haskell looks so fun!). I feel like I have only just scratched the surface because I have yet to use it in a business context. I would strongly suggest against doing it though. If you program in a language only you know then only you will be able to support it. Unless you want to ...


22

Lisp is the oldest language of these having LET now. But BASIC was the first that got it, for Lisp had obtained it much later. In Ada Lovelace Analytical Engine (1843) - no LET, a program looks as: N0 6 N1 1 N2 1 × L1 L0 S1 L0 L2 S0 L2 L0 CB?11 ' In Plankalkül of Zuse (1943-45) the program looks: P1 max3 (V0[:8.0],V1[:8.0],V2[:8.0]) → R0[:8.0] max(V0[:8....


21

In this video I watched recently, Rich Hickey comments that he likes the destructuring part of languages like Scala, but not so much the pattern matching part, and he designed Clojure accordingly. That probably explains why the pattern matching is in a library and not as robust, although the kind of problems seen in the post you mentioned are clearly bugs. ...


20

Clojure doesn't have call/cc, but you don't want undelimited continuations anyway. We argue against call/cc as a core language feature, as the distinguished control operation to implement natively relegating all others to libraries. The primitive call/cc is a bad abstraction -- in various meanings of 'bad' shown below, -- and its capture of the ...


20

Lisp dialects have a unique niche due to their simple syntax. This simple syntax makes meta programming through the use of macros very simple. This allows you to model the language to suit your problem domain instead of modeling to fit your problem domain into your language. It's really cool once you get your head around it, and it lets you do a lot of ...


18

Short Answer: There is a simple solution to your problem. Just install the virtual-machine with Linux (Ubuntu) - it is free. I am also a mainly .NET developer who loves to experiment different things, or try variety of frameworks and/or development tools. The main thing here is not to mess your main development environment. Thus, installing everything in a ...


18

So from a neutral point of view, which syntax is simpler and readable? Those are two very different questions. Simpler means "composed of fewer parts". Clojure's syntax has fewer rules, ergo it is objectively simpler than Scala's. Readability, however, is subjective. It mainly comes down to familiarity. For example, I find ECMAScript, C#, Java, D, Go, C++ ...


17

I'm sure if I leave the team, --not saying I'm leaving. :)-- no one would maintain it. Possibly False. If the program has value, and management sees the value, they will task someone with learning Clojure and maintaining it. Happens all the time. This program will be destroyed and some will develop with other language. Always true. So why worry about ...


16

A decorator is basically just a function. Example in Common Lisp: (defun attributes (keywords function) (loop for (key value) in keywords do (setf (get function key) value)) function) In above the function is a symbol (which would be returned by DEFUN) and we put the attributes on the symbol's property list. Now we can write it around a ...


15

Actually Java 8 doesn't introduce much that will be detrimental to other JVM languages that interop with Java. The work done on Lambdas helped fix a number of small issues around invokedynamic, MethodHandles , MethodReferences etc - but apart from that it's carry on as normal. That said, there's a whole new bunch of APIs that the other JVM languages could ...


15

First, a disclaimer: I'm not deeply familiar with the uniform access principle so you may want to take this with a grain of salt. That said, I would argue that Clojure does observe a uniform access principle: function calls. The key quote on Wikipedia seems to be that "all services offered by a module should be available through a uniform notation, which ...


14

This might be controversial, but my advice would be to write as FEW comments as possible. Use nice, clear class names, variable names and method names instead. Write your code in the clearest way that you can; and consider this to be the most important attribute of your code (other than that it meets its requirements). Only write a comment if you've made ...


14

Well, between those three, Lisp definitely had it first. Haskell came about in the 80s, and Clojure in the 00s, and let had been around long before either of those dates. :-) As to whether Lisp was the language to have invented it, I can't vouch for that yet, but I'll do some research and see. :-) Update: According to Evolution of Lisp (see page 46), it ...


14

I think slide 13 at his presentation (The Value of Values) helps to understand this: Values Don’t need methods I can send you values without code and you are fine My understanding is, Hickey suggests that if I need to, say, double the value you sent to me, I simply write code looking like MyValue = Double(YourValue) You see, ...


14

In my first year at the university the first programming course I took was using SICP (that was 1988). However, I had a similar problem like you: for my own computer (actually a Commodore Amiga), there was no Scheme implementation available at that time, only a different Lisp dialect (I don't remember its name). However, given the flexibility of Lisp, it ...


13

They don't avoid them, they embrace them using the pattern match syntax. But functional programming is largely orthogonal to object oriented programming, so absolute majority of "functional" languages are also object oriented¹, including clojure. In fact clojure's multi-methods are even better than plain virtual methods of Java, because they can dynamically ...


13

In case you didn't know it already take this insight: The concepts of object-oriented and closures are two sides of the same coin. That said, what is a closure? It takes variable(s) or data from surrounding scope and binds to it inside the function, or from an OO-perspective you effectively do the same thing when you, for example, pass something into a ...


Only top voted, non community-wiki answers of a minimum length are eligible