Hot answers tagged

8

Mason's answer is correct, and yes you should read my series. To emphasize his point more thoroughly: Can a monad be thought of as a function that accepts a value and wraps it such that it meets specific interface and behavioral constraints Yes, but that is not the best way to characterize a monad. You are very close though; we can make small changes ...


6

Objects are about functional abstraction and dynamic dispatch. Functional abstraction is obviously trivial in functional languages. Dynamic dispatch can be implemented using first-class functions, again, those should be available in a functional language. Basically, an object is a dispatch function which returns functions (methods) which are nested in a ...


6

What is the connection between type inference and advanced type systems? There are three things off the top of my head: Language designers interested in type systems will focus their language design around them. This means that all sorts of type oriented features were considered/implemented - inference being one. ML had it, so when follow up languages ...


6

Eric Lippert wrote a great series on monads that actually explains them in a way that makes sense to non-Haskellers. The whole thing is worth reading, but the basic idea is as follows: A monad is a "type enhancer" that takes a basic type and does something new to it, (such as turning a type T into a sequence like IEnumerable<T>,) with the following ...


5

Just split the list into a scalar value to process separately, then work on the rest. val first = list.head val rest = list.tail val firstResult = firstFunc(first) val restResult = rest.map(restFunc)


3

The short answer is you just copy it into a container, and rebuild the container when the original object changes. This isn't as burdensome as you might think. In other paradigms, we accept the tight coupling of a mutable reference because of the fear the object we are referring to will change and we will not pick up that change. With immutability that ...


3

Typeclasses instances are effectively implicit parameters containing a dictionary of functions. This makes them different from interfaces in a few ways. One is that you don't need an actual instance on which to invoke operations, and resolution can happen at the type level instead of the value level. The Read class is an example: class Read a where read :...


3

Neither. Usually in this situation, you do not want to mutate x, but you do want to take advantage of it already being in scope. This gives you: def f(x): def h(): <do something using x> return y def g(y): <do something else> return z return g(h()) These functions are very small typically, and ...


2

I sketch a possible approach. I would first define the operations that are expected to be applied to your data structure (if I understand correctly, you have already done this). Using Haskell notation (I am not familiar with F#, I hope the Haskell notation is intuitive enough for you) for lists you would have: add :: a -> [a] -> [a] remove :...


2

Your option 1 allows x, y, and z to be bound only once, and remain immutable. Many types of languages are moving in this direction, for example, Swift (a strongly typed language) has a let statement that introduces a variable with an immutable binding; Javascript (a dynamically or untyped language depending on how you look at it) has a const declaration for ...


2

The biggest difference is that type classes are open, and interfaces are closed. What I mean is that if you have some 3rd party type, if it doesn't implement an interface then you're out of luck. You get to extend it or adapt it into a new type which can then implement the interface you want. If you have some 3rd party type that doesn't implement a type ...


2

To answer your question more explicitly: no, this is not subtype polymorphism, and the reason is simply this: each type that has an instance of a typeclass is distinct. Each has different functions with (potentially) different signatures, for example if we have the following types: data First = First String deriving Eq data Second = Second Int ...


2

There's nothing missing from the language itself that would prevent you from doing functional programming. The only thing missing from the runtime is tail call optimization, and you can actually do quite a bit of FP before hitting that limitation. What's really going to hurt you if you try to do FP in PHP is lack of library support for it. You need a ...


2

i) its a function that takes a type and returns a new type i.e. a generic type, i'm not sure it would directly have a use in js as you don't have types iii) would return a different type but in the same monad i.e. you can reproject from m a -> m b it's a minimal definition however there are usually other functions defiend as well e.g. every monad is a ...


2

I would try something like that in Scala for mapping differently the first element : def apply[T,X](f: T=>X, g: T=>X, l: Seq[T]): Seq[X] = l match { case Nil => Nil case head :: tail => f(head) +: apply(g,g,tail) } > apply((x: Int)=> x*x, (x: Int)=> -x, List(5,4,3,2,1)) res1: Seq[Int] = List(25, -4, -3, -2, -1) The idea is to ...


1

Can a monad be thought of as a function that accepts a value and wraps it such that it meets specific interface and behavioral constraints that have been found to be useful when working in a functional style? No. The function that wraps a value is unit. If you're trying to see if it makes sense to explain monads as a unit function that returns an ...


1

I have a stream parsing method in C# that reads portions of a protocol frame from the STOMP protocol... Can anyone give some examples of processing in a functional manner? You're thinking about this at the wrong level. The interesting question is not "how would I implement this low-level byte-buffer code in a functional style?" The private ...


1

A singly-linked list is the simplest persistent data structure. Persistent data structures are essential for performant, purely functional programming.


1

In computer science, a function or expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world. From Wikipedia - Side Effect A function, in the mathematical sense, is a mapping from input to output. The intended effect of calling a function is for it to map the input ...



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