Connecting the first steps

* snip snip .. this was alot longer than i expected.. ill rewrite **

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

shorter version: im ignoring

shorter version: im ignoring statefullness and monads here

first post was miles long so i editted it :)
Im wondering if im on the right track regarding my understanding of FP and stuff like closures. This post is to get some judging feedback regarding the correctness of my thoughts.

a function
is a body of functionality such as 10+9 or 2/3

a closure
is a way to introduce variables into a function x y = x+y or x y = x/y. Closures have bound variables meaning that the x and y in x/y are fixed.
this means that x y = x + y + z is not a closure since z is not an argument

a lambda
is an anonymous pointer to reference a closure
lambda x y = x + y
these lambdas can be provided as an argument to another lambda or method

a method
is a named pointer to reference a closure
add x y = x + y

I dont understand which one of the lambda or method introduces type safety or that that type safety is deferred from the + operator in the function body
add:: int -> int -> int is typed .. is that possible for a lambda as well ?

These 4 items (function, closure, lambda and method) are ways of performing operations on an element. An element is one atomic thing being a simple value or some complex such as an object or a structure. Besides those atomic elements there is another atomic element with the capability to store other elements within it. Lists. I can put elements in a list. I can put lists in a list, but i cant put elements into a nonlist element.
element = EMPTY or 1
list = N times element where N > 0 and list is not atomic.
Meaning our world is made up as : everything is either (EMPTY or 1) or N ... atomic or aggregate .. ( 1 /\ N )

Now that we have a way of defining operations in conjunction with elements and lists we can start making methods.
There are 4 types of method.
1 -> 1 :: Methods that manipulate an input of 1 to a result of 1
N -> N :: Methods that manipulate every item in a list. The list does not change in size
N -> M :: Methods that manipulate the shape of a list where the result is a list.
N -> 1 :: Methods that manipulate a list in such a way that there is only an atomic 1 element result .
1 -> N :: Methods that transform an atomic 1 element into a list.

1 -> 1 example :> incr int -> int { return result of operation f on atomic element }
incr 10 = 11

N -> N example :> map [A] -> (A ->B) -> [B] { foreach element in list return result of operation f on atomic element
map [1,2,3] IntToString = [one, two, three]

N -> M example :> filter [A] -> (A -> Boolean ) -> [A] { foreacht element in list only return element if operation f on element evaluates to true }
filter [1,2,3, 4] IsEven = [2, 4]

N -> 1 example :> foldr [A] -> (A -> B -> B), B) { foreach element in list combine element with the recursive result the other elements }
foldr [1 .. 10] + = 55

1 -> N example :> repeat A -> int -> [A]
repeat 5 4 = [5,5,5,5]

So summing up
1 ) there are functionalities inside closures. Closures are referenced by either anonymous lambdas or named methods.
2 ) there are elements and lists of elements. 1 OR N
3 ) there are only 5 kinds of method. 1->1, N->N, N->M, N->1, 1->N

Are these 3 rules correct and most of the stuff i need to understand ?

PS : And yes i understand that there are more kinds of methods like cons that add element x to the list xs, or that concat 2 lists into 1 list. Basically i consider the former as 1 -> N and the latter as N -> M
Since in the case of cons the intent is at ELEMENT level. to manipulate the value of the element. In the case of concat the intent is on LIST (N ) level to manipulate the shape of the list where the list is a collection of elements.


Im wondering if im on the right track regarding my understanding of FP and stuff like closures.

The answer is no but this is the wrong way and the wrong place to ask this question. Better places would be the mailing lists, IRC channels, whatever of a particular functional language such as Haskell, O'Caml, SML, or Scheme; or the newsgroup comp.lang.functional.


I am not sure what you are trying to do. Are you trying to invent a theory for functional programming based on some casual reading you did? Or are you trying to learn what functional programming really is? Or maybe none of the above?

If you want to learn what functional programming is, I suggest using a textbook (if you need suggestions look at the getting started thread, or ask). If you are following a textbook, let us know which so we may be able to offer some more concrete suggestions.

im not studying this in a

im not studying this in a casual manner. I have read some books via my study and from bookstores.

Thing is that i have some idea that there is a good way to connect alot of different ideas as they are not all that different.

A programming language is about calling bodies of functionality. What I feel is important is to undertand how that works. How is it that it can function. And are there other ways of expressing those concepts.
If stuff is possible in mathematics and there is a language that has many things in common with mathematics, then to what extent can I apply mathematics on that language.

There is also alot of difference between for instance haskell and java. But it must be possible to introduce concepts into a java framework to make it possible to have a system reason just as if its haskell. Now dont get me wrong i have in no way the plan to make haskell in java or .net. But I intend to apply java in a way which closely matches haskell.

Why are there generics ? Well in my opinion generics exist because in some cases you dont want or have to know the type of what you are manipulating. BUT you DO want typesafety (thus generic types).

You can construct closures in java (to a limited degree and its a responsibility of the programmer to not affect things outside the scope of the method)

You can construct lazylists (al be it in a dirty way) in java hence creating lazy evaluation (contrary to yield return in .Net).

A lambda is merely a name (albeit an anonymous one) to call some body of code. Though you lack bind operators etc.

So my point is to grasp the concepts that are involved. The ways of thinking. Only when I understand that I can introducing those concepts in a proper way into a system that doesnt natively support it. And yes I understand that java or .net never can fully operate as haskell and all as they are now, but it IS possible to provide construcs to work in a similar way ...


on top of that I am trying to generalize the bulk of code that I write.
If you have a method signature such as

public Object methodname(List xs, Func then i can predict how the body looks. we have something that resembles N, we have some operation that works on something that is 1 and result in a predicate true or false. the overall result of the method is a 1. So teh method body must at least contain:
1 ) a foreach(Item i in xs) construct
2 ) a testing construct since the func tests an element for a property
3 ) a selection in order to return a 1
so probably this method is a way of selecting or finding an element.
we go from a N to a 1.

if we want to be able to go from A -> [B] it would not be possible unless you would want to iterate forever like the repeat method. In order to go from a 1 to a N (something that is not something taht knows how to be iterable to something that does know how to be iterable) we need to introduce a way to tokenize that element A.
So that method would require an introduction of knowledge about how to split that oneelement A.
Take the
public String[] String.split(String sourceString, char splitString) method in java.
we go from 1 sourcestring into N strings by applying a way of splitting up a given string into multiple strings. We can also see that the method signature is a bit off since we dont introduce functionality (a func that does the splitting), but an argument splitstring for the splitting operation. So that means that the functionality to decide how to split is hardcoded in the body of the split method. And that the split method isnt reusable in other contexts.

But its clear from the split signature that there will be looping over the inputstring. that there will be a comparison and that the original string will be cut up into smaller parts.

its hard to explain.

Study the lambda calculus.

Study the lambda calculus. Closures are an implementation artefact, and it's possible to envisage implementations of functional languages that don't use them.


In particular, I would suggest reading Benjamin Pierce" "Types and Programming Languages", which does a good job in explaining lambda calculus as well as a wide range of type system issues. Plus, it comes with toy implementations.