Hello everyone, im starting on computer science the coming summer. I checked our classes schedule and we start of by Functional Programming in SML. I download the book we gonna use but I cant make sense of it.

I guess my question is what do you use 1.Functinal programming for, 2. how do you use it, 3. can you make programs/Apps in it like you can in C++ or 4. is it pure teoratical stuff??

5. How does MatLab differe from SML, or 6. are they completely 2 different tools that got nothing to do with each other.. I hope a friendly soul can help me out :)

Best regards

I could tell you that functional programming (FP) is a style of programming in which one never changes a value that has already been computed. Instead, one creates new values as needed. By implication, using FP in practice requires a good garbage collector in order to get rid of values …

## All 7 Replies

I could tell you that functional programming (FP) is a style of programming in which one never changes a value that has already been computed. Instead, one creates new values as needed. By implication, using FP in practice requires a good garbage collector in order to get rid of values that are no longer needed.

Here's a simple example in C:

``````int factorial (int n) {
int result = 1;
while (n > 1) {
result *= n;
--n;
}
return result;
}``````

This code does not use FP, because it has the side effect of modifying the variables named n and result each time through the loop.

It is possible to achieve the same effect in an FP style, even in C, as follows:

``````int factorial(int n) {
if (n <= 1)
return 1;
return factorial(n-1) * n;
}``````

You will see that in this second example, no variable is ever changed after being given its initial value, so there are no side effects.

The foregoing explanation is grossly oversimplified, and I can think of no short way to explain why FP is important or useful, but this is a start.

There are several programming languages in widespread use that make FP easy (or sometimes even necessary). The three best known are probably Lisp (particularly its Scheme dialect), Haskell, and SML. There are substantial differences between the three languages, but it's not easy to explain those difference in a small space.

As far as I know, SML and Matlab are unrelated.

Fair enough ty for the reply but what is it used for specifc .. Is the language supportive for other languages...

And by Matlab I meant that if Matlab is also Functional Language or/and what is Matlab used for.
Im sry maybe my logic isnt good but I need very cut out specific answers... In human language lol :)

I did functional programming first semester this year, and we also did SML... don't know anything about MatLab, but I'll answer everything I can.

arkoenig was right in what he told you about values never changing, it's very different to what we usually use. However SML has implemented certain kinds of loops since they can be changed into recursion.

Really what it comes down to is that you use a lot of recursion rather than iteration - functions will call themselves over and over, as you can see in arkoenig's code. Now for your questions...

1) Functional programming can be used for anything that 'traditional' programming can be used for, it has the same capabilities. I don't know if functional languages usually support user interfaces, but other than that they're incredibly powerful, as well as very elegant.

2) Well you'll be taught how to use it of course, but basically everything you do must be turned into a function. You have no global variables, and you have a very limited set of operations. But in spite of that it's very powerful, and a joy to work with once you understand it.

3) You can make apps with it, although you will often make the front-end (User interface etc.) in something else, and link through to your functional to compute results. Why? Functional languages tend to be very fast, and the use of functions makes them very easy to debug as well.

4) No, it is not purely theoretical, although you will learn some amazing theory. Also, you will love recursion after this course.

5 & 6) As I mentioned I don't know MatLab, although I suspect the two have no relation.

Hope this helps, and feel free to ask more if you need to.

Keep well,
M

commented: Ty for explaining +1

I did functional programming first semester this year, and we also did SML... don't know anything about MatLab, but I'll answer everything I can.

arkoenig was right in what he told you about values never changing, it's very different to what we usually use. However SML has implemented certain kinds of loops since they can be changed into recursion.

Really what it comes down to is that you use a lot of recursion rather than iteration - functions will call themselves over and over, as you can see in arkoenig's code. Now for your questions...

1) Functional programming can be used for anything that 'traditional' programming can be used for, it has the same capabilities. I don't know if functional languages usually support user interfaces, but other than that they're incredibly powerful, as well as very elegant.

2) Well you'll be taught how to use it of course, but basically everything you do must be turned into a function. You have no global variables, and you have a very limited set of operations. But in spite of that it's very powerful, and a joy to work with once you understand it.

3) You can make apps with it, although you will often make the front-end (User interface etc.) in something else, and link through to your functional to compute results. Why? Functional languages tend to be very fast, and the use of functions makes them very easy to debug as well.

4) No, it is not purely theoretical, although you will learn some amazing theory. Also, you will love recursion after this course.

5 & 6) As I mentioned I don't know MatLab, although I suspect the two have no relation.

Hope this helps, and feel free to ask more if you need to.

Keep well,
M

I sincerly thank you both for the help. I was worried that it was just boring theorical stuff... I guess the next thing to learn after c++ is functional programming since we are gonna have it in the course. Ty so much, words cant express it :)

That's good.

Hi

I haven't had much experience of Matlab but I have used it for image compression and sound recognition before. I'm not sure if it comes under the functional programming catogery. It works with matrices and is effectivly a large calculator that is great for working with math problems. Initially you can ctually just type in 4+6 and hit return and it will reply 10.

There is no need to declare variables even to start with (although you do need to store them for reuse).

It is much nicer then languages such as C for image manipulation. You can read in an image and it will automatically generate the image matrix and give you access to histograms etc.

You can then in Matlab simply say image/2; and it will divide all the number by 2 making the image darker. You can then add more advanced function to scan across the image and look for lines in images etc.

It also allows for easy scripts to be run as it has its own functionality for it.

I wouldn't reccomend looking into to much if you aren't going to use it initally as it has quite a differnet thought process to other languages.

Thx alot arkonig, swineflue and eslimo, I apreciate it alot :). Helped clearing some of the concepts I didnt understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.