0

I am trying to get better at functional programming. As a start I am planning on trying out with couple of languages like Pascal, Scheme, ML etc. First I started with Pascal. I am trying to insert user input into a integer array in pascal and then get them reverse.

  1 program ReverseList;
  2 
  3 var
  4         i: Integer;
  5         k: Integer;
  6         a: array[1..100] of Integer;
  7 begin   
  8         i := 0;
  9         repeat  
 10                 writeln('Enter a number');
 11                 readln(k);
 12                 if k > -1 then 
 13                         i := i + 1;
 14                         a[i] := k;
 15         until(k < 0);
 16         for i := 1 to i do    
 17                 writeln(a[i]);
 18 end.

In past I have mostly been a java developer so I was so custom to using all the lists thats available. Also ideally I was wondering if I can build a list where I can iterate over the list based on the number of elements in that list.

It would be great if anyone could point me on the direction of good tutorials in functional programming as well as syntax on above mentioned programming languages.

8
  • maybe you accept some answers that were helpful first!!! Commented Sep 25, 2011 at 17:51
  • 1
    Good to know didn't know that was a critical factor, I ll keep that in mind going forward. Thanks. Commented Sep 25, 2011 at 18:15
  • I have given points and verbally thank using comments but didn't realized about the acceptance. Commented Sep 25, 2011 at 18:45
  • 1
    @evilone, its not the key point. I agree that it is fair to give credit to those who have helped you, but, you still aren't required to do so. Commented Sep 25, 2011 at 18:50
  • 1
    @Null-Hypothesis, if you want more answers from other people, its maybe better not to answer (and accept) your own question. If you have the correct solution and you don't want any more answers, you can provide the answer yourself. Commented Sep 26, 2011 at 19:00

2 Answers 2

2

There are several problems with your program:

  • The array is not initialized.
  • There is no input checking, both i=0 and i>100 result in an illegal array index.
  • The array index and the value are the same, is that correct?
  • You only write the first 10 numbers (but you use a different index, which is certain to be out of range).
  • The output is not in reverse.

There are also several pascal tutorials.

By the way, Pascal isn't a functional language. So if you really want to learn a functional language, you better try another one (like Lisp, Ml or probably F#).

Sign up to request clarification or add additional context in comments.

2 Comments

I actually updated the code section of the question after your comment. Now I have a list printing and working on the reverse. Thanks much.
Gamecat: he said learning functional programming, not learning function language :-)
0

It was a good practice and I managed to figure a solution for this. I am sure there are better ways to do, and also this doesn't look like I am using the functionalities of functional programming. But if anyone wants to provide a better solution please do so,

{author: Null-Hypothesis}

program ReverseList;

var
        i: Integer; {integer to keep the array length}
        k: Integer; {user input value}
        a: array[1..100] of Integer; {array to store the user inputs}
begin
        i := 0;
        repeat  {iterate until user input is negative or number of inputs exceed array size}

                writeln('Enter a number or enter negative value to exit the program.');
                readln(k);
                if(k > -1) and (i < 100) then   {check for negative value and size of the array}
                begin
                        i := i + 1;     {increase array index}
                        a[i] := k       {assign value to array}
                end
                else
                        break;  {exit if array size exceed the limit of array}
        until(k < 0);
writeln;

{Printing the user input before the reversing the list}
writeln('Original order of the list');
for i := 1 to i do
        writeln(a[i]);
writeln;

{Printing the reverse list}
writeln('Reversed List');
for i := i downto 1 do  {decrement array index}
        writeln(a[i]);
writeln('Bye!!!');
end.

Happy Coding, off to the next language...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.