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 ...


40

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.


35

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": "...


31

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 ...


24

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

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

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 ...


20

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 ...


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++ ...


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

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 ...


13

I really like Jules' answer, but I wanted to additionally point out something people often miss about lazy functional programming, which is that everything doesn't have to be "inside the loop." For example: baseSums = scanl (+) 0 offsets = scanl (\offset sum -> if sum == 150 then offset + 100 else offset) 0 zipWithOffsets xs = zipWith (+) xs (offsets ...


12

I use Haskell and Scala (and also doing some Scheme, Common Lisp, SML, OCaml, Erlang) all under Linux (Ubuntu). All of the above languages (compilers and interpreters) can be installed quite easily (there are packages that can be installed using apt-get or another package management system). Scala has a good integration with Eclipse and the Scala plugin(s) ...


12

They do make computers like that. It's called an FPGA. Of course, FPGAs support both sequential and combinational logic, but there's nothing preventing you from just using the combinational portion as you're suggesting. In practice, however, sequential logic (the kind with state) is extremely useful even at the chip level. For one thing, it significantly ...


12

Exagg'itive Summary (TM) You get a few things. Prototypal inheritance and cloning Dynamic addition of new properties Co-existence of objects of different versions (specification levels) of the same class. Objects belonging to the more recent versions (specification levels) will have extra "optional" properties. Introspection of properties, old and new ...


10

The strengths of Clojure have been highlighted many times, and, sure enough, make for a very powerful language, so I won't repeat them again here. I always loved lisp, since the uni days and I was really excited when I discovered Clojure (perhaps the chance of using lisp outside of academia at last!). However as I started learning an uneasy feeling has ...


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