Tag Info

New answers tagged

0

Mind that functional languages use data structures and separated functions instead of objects. For example you would have a set of rooms and and inventory as a world instead. Ideally you would also limit the amount of data you give to functions to how much they actually require as much as possible instead of passing the entire world (say you extract Room D ...


1

I think you need to describe side-effects first. Before you can test for them, you need to define them. If you define a side-effect as changing the program's state, functions that contain only local variables and do not change an object or system's state external to that function are side-effect free. These can easily be marked in C++ using the const ...


2

In JavaScript a constructor is just an ordinary function. Any function which can be called as a constructor with 'new' can be called as an ordinary function. But functions will almost always be written as either a constructor or non-constructor function, and using it the other way is an error. The built-in objects however are specifically designed so that ...


1

Because of the way the expression is parsed and evaluated, a sum function will get the results of the EnumFromTo, and therefore under normal conditions there is no way to get at its arguments. To receive the unevaluated arguments, you would need to use a macro. I don't know anything about Template Haskell, but supposedly it provides this ability. In ...


-2

Check this (Clojure code, sorry I don't know Haskell): (declare m-enum-upto) (defn enum-upto [n] (cond (= n 0) 0 (= n 1) 1 :else (+ n (m-enum-upto (dec n))))) (def m-enum-upto (memoize enum-upto)) ; at the first run (enum-upto 5) ; 0.497711 msec ;again (enum-upto 5); 0.1130686 msec ;and now (enum-upto 3); 0.09372 msec (enum-upto 30); ...


5

Even though you said algebraic data types, you seem to mostly be asking about sum types, so I will focus on those. Product types are more common and more easily understood. Sum types are most easily understood not by thinking about what you're modeling, but by thinking about the code that uses it. People tend to think of sum types as representing states ...


3

Do you know of guidelines and best practices that guide through modelling logic in types properly? Sure, at least in theory. Algebraic Data Types are made of two parts, sum types (or variants, or discriminated unions, or...) and product types (or tuples; conceptually records and classes fall here, but some disagree). Product Types are used when you ...


2

Some functional languages such as OCaml have built-in mechanisms to implement abstract data types therefore enforcing some invariants. Languages which do not have such mechanisms rely on the user “not looking under the carpet” to enforce the invariants. Abstract data types in OCaml In OCaml, modules are used to structure a program. A module has an ...


4

Encapsulation is not a feature that came with OOP. Any language that supports proper modularization has it. Here's roughly how you do it in Haskell: -- Rational.hs module Rational ( -- This is the export list. Functions not in this list aren't visible to importers. Rational, -- Exports the data type, but not its constructor. ratio, ...


2

You do it the same way: make a constructor that enforces the constraint, and agree to use that constructor whenever you create a new value. multiply lhs rhs = ReducedFraction (lhs.num * rhs.num) (lhs.denom * rhs.denom) But Karl, in OOP you don't have to agree to use the constructor. Oh really? class Fraction: ... Fraction multiply(Fraction lhs, ...


-1

Note how the input is a Free DSL a, for any type a. The only way to produce that with out particular DSL is by ending the expression with End—which guarantees that we can't forget to close the connection once we're done. Not true as shown above. In this form the caller can pick any type for a.


0

Think about how you would implement a "state machine" in a programming language without state. You could probably actually do it but you would end up using function names as storage. Ending up with gobblyday gook like: if (sm.atBegining()) sm.start() else if (sm.done()) sm.stop() ) else sm.progress()


0

You can avoid explicit mutable state as long as you don't have to interact with the outside world. In JavaScript in order for you program to actually have an effect beyond taking up processor cycles, you have to modify the Dom or the Window object, and these API's are stateful. But I suppose you could create a wrapper which passed the Dom and Window objects ...


0

State means the ability to respond to a present stimulus in an manner that depends on past stimuli, not just based on the present stimulus. Purely functional programs are just functions. Thus for practical applications the purely functional program inputs a pair (old_state * present_stimulus) and outputs a pair (new_state * present_response). An external, ...


5

The real defining characteristic that separates declarative from imperative programming is in declarative style you are not giving sequenced instructions; at the lower level yes the CPU operates this way, but that's a concern of the compiler. You suggest CSS is a "declarative language", I wouldn't call it a language at all. It's a data structure format, ...


9

The basic unit of an imperative program is the statement. Statements are executed for their side effects. They modify the state they receive. A sequence of statements is a sequence of commands, denoting do this then do that. The programmer specifies the exact order to perform the computation. This is what people mean by telling the computer how to do ...


0

You are right that there is no clear distinction between telling the computer what to do and how to do it. However, on one side of the spectrum you almost exclusively think about how to manipulate memory. That is, to solve a problem, you present it to the computer in a form like "set this memory location to x, then set that memory location to y, jump to ...


9

You seem to be drawing a line between declaring things and instructing a machine. There is no such hard-and-fast separation. Because the machine that's being instructed in imperative programming need not be physical hardware, there is much freedom for interpretation. Almost everything can be seen as an explicit program for the right abstract machine. For ...


0

Myself, I would definitely look into the ability of the language in question to access some form of database. Most of the events that happen to change the state of the world concurrently would simply be recorded to disk, and would not affect the current player within the current room (outside of special circumstances such as explosions, or in an MMO, ...


1

You can write functions even when using classes. You said you created static functions, so there is no "state". When you move those methods to their respective classes, of course they can't continue to be static, they need to be regular instance methods. But if you don't modify any of the parameters, even the receiver of the message (which is the same as ...


2

I don't think that simply using static methods makes it Functional I don't think simply moving your previously static methods to a class makes it OO If you are doing OO, then your classes should have properties and the methods should change those properties. This makes the 'data object' mutable and functional programming goes out the window While Static ...


6

However, I lose the benefits of functional style. If you always lose the benefits of functional style code when converting bundles of functions to objects, you're doing it wrong (or have an usual idea of what the benefits of functional style are). After all, instance functions are the same thing as static functions but simply have an additional ...


5

What is it about Haskell that has led to it's rise in popularity among experts in the FP world? There's a few of different things I've seen: It's novel. As much as FP enthusiasts poo-poo all of the fads in imperative and OO programming, they're still human. Lisp has been around since the 60's. ML since the 70's. A lot of people have spent a lot of ...


2

I'd go with the second approach, with a change which releases the handle when done. If each method takes care of getting, operating and releasing its own handle, then your application should be better suited to scale up (assuming you have some sort of pooling underneath). With the first approach, it is hard to say what will happen should two different ...


4

Hm, well for the first one this is because let's say f f was well typed. We're applying f so it has to be a function. Functions have the type f : A -> B for some A and B. Since f is also the argument, it must also be the case that f : A. This means that A = A -> B. Moreover since f = f f, the the type of f f is also the type of f. Since f f : B this ...


13

It's called arrow token Here on wiki, this article and that article or even on java website Many years ago, before there were any computers, the logician Alonzo Church wanted to formalize what it means for a mathematical function to be effectively computable. (Curiously, there are functions that are known to exist, but nobody knows how to compute ...



Top 50 recent answers are included