12 Replies - 1442 Views - Last Post: 09 January 2010 - 01:34 PM

#1 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Functional Programming Advice

Posted 01 January 2010 - 04:26 AM

Hello ,I am from Romania. I am rather new in using functional programming! Our teacher gave each of us an assignment.Mine says that I have to find all the natural divisors of a natural number,greater than 0. I am currently working in Poly/ML.I know already that in solving this problem ,a recursive approach is needed.I tried to put up my algorithm in ML,but ,when compiled, my program throws only one solution instead of al the divisors.In my algorithm I use a function called "divizor" and two parameters called n and i. "n" stands for the number which is given for finding its divisors and "i" stands for the first divisor that the program should start with. Here is the code I used in Poly/ML:
-------------------------------------------------------------------------------------------
exception NumarNegativ;
exception OutOfBounds;
fun divizor(n,i)=if n<0 then raise(NumarNegativ)
else if n mod i =0 then i

else divizor(n,i+1);
--------------------------------------------------------------------------------------------
The two exceptions that I created ar justified by the fact that if a negative number is given, then the program throws an exception ,telling that the number introduced is a negative one.The secound exception makes sence for stopping the recursion.
Could you please give me some ideas or guide me towards what there is to correct in my way of dealing with this problem?
Thank you in advance!!!! :^:

Is This A Good Question/Topic? 0
  • +

Replies To: Functional Programming Advice

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Functional Programming Advice

Posted 01 January 2010 - 11:12 AM

Moved to Functional Programming.
Was This Post Helpful? 0
  • +
  • -

#3 YamNad  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 120
  • Joined: 11-July 09

Re: Functional Programming Advice

Posted 01 January 2010 - 12:06 PM

It's going to stop here:

else if n mod i =0 then i


The modulus is the remainder and if the remainder is 0 then it's a divisor and you aren't calling the function again.

I think that's correct.
Was This Post Helpful? 0
  • +
  • -

#4 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Functional Programming Advice

Posted 01 January 2010 - 02:34 PM

View PostYamNad, on 1 Jan, 2010 - 11:06 AM, said:

It's going to stop here:

else if n mod i =0 then i


The modulus is the remainder and if the remainder is 0 then it's a divisor and you aren't calling the function again.

I think that's correct.


That's right. I can't provide code because I'm a Haskell and Clojure guy, but not a ML guy. What you need to do is add a second argument to your function, which would originally be passed an empty list. Then your code would call itself, concatenating i to the list for every divisor.
Was This Post Helpful? 1
  • +
  • -

#5 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Re: Functional Programming Advice

Posted 02 January 2010 - 08:38 AM

View PostRaynes, on 1 Jan, 2010 - 01:34 PM, said:

View PostYamNad, on 1 Jan, 2010 - 11:06 AM, said:

It's going to stop here:

else if n mod i =0 then i


The modulus is the remainder and if the remainder is 0 then it's a divisor and you aren't calling the function again.

I think that's correct.


That's right. I can't provide code because I'm a Haskell and Clojure guy, but not a ML guy. What you need to do is add a second argument to your function, which would originally be passed an empty list. Then your code would call itself, concatenating i to the list for every divisor.


I have yet another question.Is it possible for instance, within the "if" instruction ,on the "then" clause to put two or more instructions? .
P.S:Thx for your help !
Was This Post Helpful? 0
  • +
  • -

#6 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Functional Programming Advice

Posted 02 January 2010 - 12:40 PM

View Postvolcano992, on 2 Jan, 2010 - 07:38 AM, said:

View PostRaynes, on 1 Jan, 2010 - 01:34 PM, said:

View PostYamNad, on 1 Jan, 2010 - 11:06 AM, said:

It's going to stop here:

else if n mod i =0 then i


The modulus is the remainder and if the remainder is 0 then it's a divisor and you aren't calling the function again.

I think that's correct.


That's right. I can't provide code because I'm a Haskell and Clojure guy, but not a ML guy. What you need to do is add a second argument to your function, which would originally be passed an empty list. Then your code would call itself, concatenating i to the list for every divisor.


I have yet another question.Is it possible for instance, within the "if" instruction ,on the "then" clause to put two or more instructions? .
P.S:Thx for your help !


Well, the thing here is that you're thinking imperatively in a functional language. In functional languages, it's better to thing of things in terms of expressions. If ML is anything like Haskell (and it is in several ways) everything is an expression, so everything returns a value. When you think of everything as an expression that returns a value, multiple 'statements' doesn't make sense. The only reason you would want to execute an expression but NOT return it's value is if you were executing that statement for it's side effect. I'm not sure how pure of a functional language ML is, so I'm playing by ear here. ;)

Show me what you're trying to do and I can probably show you a way to do it. Also, if you're wanting to assign something to a variable, and then use that variable in another expression, I'm sure there is a way to do that.

EDIT: After a little research, I've found out a little more about ML. ML doesn't encapsulate side effects like Haskell, so it's impure. So I guess you should be able to put as many expressions as you like in the 'then' clause, but it's pointless unless you're doing it for their side effects. When expressions always return a value, and you don't need to execute side effects, you never need more than a single 'instruction', because you can pass the values of expressions as arguments to functions and all that good stuff. So yeah, show me what you're trying to do, and I can probably help you.

This post has been edited by Raynes: 02 January 2010 - 12:44 PM

Was This Post Helpful? 1
  • +
  • -

#7 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Re: Functional Programming Advice

Posted 03 January 2010 - 01:18 AM

View PostRaynes, on 2 Jan, 2010 - 11:40 AM, said:

View Postvolcano992, on 2 Jan, 2010 - 07:38 AM, said:

View PostRaynes, on 1 Jan, 2010 - 01:34 PM, said:

View PostYamNad, on 1 Jan, 2010 - 11:06 AM, said:

It's going to stop here:

else if n mod i =0 then i


The modulus is the remainder and if the remainder is 0 then it's a divisor and you aren't calling the function again.

I think that's correct.


That's right. I can't provide code because I'm a Haskell and Clojure guy, but not a ML guy. What you need to do is add a second argument to your function, which would originally be passed an empty list. Then your code would call itself, concatenating i to the list for every divisor.


I have yet another question.Is it possible for instance, within the "if" instruction ,on the "then" clause to put two or more instructions? .
P.S:Thx for your help !


Well, the thing here is that you're thinking imperatively in a functional language. In functional languages, it's better to thing of things in terms of expressions. If ML is anything like Haskell (and it is in several ways) everything is an expression, so everything returns a value. When you think of everything as an expression that returns a value, multiple 'statements' doesn't make sense. The only reason you would want to execute an expression but NOT return it's value is if you were executing that statement for it's side effect. I'm not sure how pure of a functional language ML is, so I'm playing by ear here. ;)

Show me what you're trying to do and I can probably show you a way to do it. Also, if you're wanting to assign something to a variable, and then use that variable in another expression, I'm sure there is a way to do that.

EDIT: After a little research, I've found out a little more about ML. ML doesn't encapsulate side effects like Haskell, so it's impure. So I guess you should be able to put as many expressions as you like in the 'then' clause, but it's pointless unless you're doing it for their side effects. When expressions always return a value, and you don't need to execute side effects, you never need more than a single 'instruction', because you can pass the values of expressions as arguments to functions and all that good stuff. So yeah, show me what you're trying to do, and I can probably help you.

Hi again,
I've mastered the if clause...but now...I'm using a list to store the divisors , but for stopping the recursion I have that condition "if i>n then ...print the list".How do i print the list?
Thx for your help sofar ...u've been most helpful!;)
So this is what i've modified sofar....
exception NegativeNumber;

fun divizor(n,i)=
if n<0 then raise(NegativeNumber)
else if n mod i = 0 then ( i::[] ;
divizor(n,i+1))
else if i>n then "STOP"
else divizor(n,i+1);
The problem is...if the "STOP" remains there at the end of the execution ...only "STOP" will appear.And instead of it i'd like to view the whole list and stop the execution.
Was This Post Helpful? 0
  • +
  • -

#8 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Re: Functional Programming Advice

Posted 03 January 2010 - 02:52 AM

Hi again...i just wanted to tell u that u have been of great assistance.I eventually finished the assignment.Here is the right code.I just did what you told me to do.Thas is i made "l" which is a list , a parameter of my function and passed it along.
It works fine..and it is very efficent due to the recursion!
Thx a 1000 times!!!

P.S:
--------------------------------------------------------------------------------------
exception NegativeNumber;
val l=[];
fun divizor (n,i,l)=
if n<0 then raise(NegativeNumber)
else if n mod i = 0 then
divizor(n,i+1,i::l)
else if i>n then l
else divizor(n,i+1,l);

---------------------------------------------------------------------------------------
:bananaman:
Was This Post Helpful? 0
  • +
  • -

#9 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Re: Functional Programming Advice

Posted 03 January 2010 - 04:36 AM

Hey ...it's me again.
U said u were a Haskell guy.
Could you help me with the same assignment ,using WinHugs?
Was This Post Helpful? 0
  • +
  • -

#10 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Functional Programming Advice

Posted 03 January 2010 - 11:07 AM

Just a tip: please :code: it makes it much easier for us to debug your code when it's syntax highlighted and indented :)
Was This Post Helpful? 0
  • +
  • -

#11 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Functional Programming Advice

Posted 03 January 2010 - 11:33 AM

View Postvolcano992, on 3 Jan, 2010 - 03:36 AM, said:

Hey ...it's me again.
U said u were a Haskell guy.
Could you help me with the same assignment ,using WinHugs?


I would be happy to help you with anything you need. Why do you want to use WinHugs though? Unless you're required to, very few people use Hugs for anything. GHC is the flagship compiler, and if you download it via The Haskell Platform which I linked to in my resource thread, you get a load of helpful libraries and Cabal, the Haskell build tool.

Posted Image

Pasting between code tags makes it easier to read your code.

This post has been edited by Raynes: 03 January 2010 - 11:34 AM

Was This Post Helpful? 1
  • +
  • -

#12 volcano992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-January 10

Re: Functional Programming Advice

Posted 09 January 2010 - 01:07 PM

Thx for everything!
I managed to build my assignment eventually!
U have been of great help!
Was This Post Helpful? 0
  • +
  • -

#13 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Functional Programming Advice

Posted 09 January 2010 - 01:34 PM

View Postvolcano992, on 9 Jan, 2010 - 12:07 PM, said:

Thx for everything!
I managed to build my assignment eventually!
U have been of great help!


You are absolutely welcome. Come back anytime. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1