OO and FP [Fwd: comparison OOP and FP (and how to put them together) was: Re: need help with haskell] 
Author Message
 OO and FP [Fwd: comparison OOP and FP (and how to put them together) was: Re: need help with haskell]

[ Attached Message ]

From:
To:
Date: Sun, 10 Jan 1999 09:16:07 +0100
Local: Sun, Jan 10 1999 3:16 am
Subject: comparison OOP and FP (and how to put them together) was: Re: need help with haskell

Quote:


> [snip Haskell code]

> : I now want to replce the recurion in main and count_vowels with
> : iteration, could count_vowels be expressed with foldl?

> There is nothing like iteration in Haskell. Everything is done with
> recursion, but that does not mean that it works less efficiently:
> Because of elimination of tail recursion by the compiler/interpreter,
> there should be no increase in memory size due to pushing return addresses
> on the stack or else.

I was aware of this but I wasn't sure if recursions written by the user
are as efficient as the things which come with the standard-libraries.
But you solution led me to another question. Programming style in FP
languages.

Was my code unusual in  that sense?  Updateed with your suggestion my
count_vowel now looks like
count_vowels :: String -> Int
count_vowels = foldl' (\acc ch -> if is_vowel ch then 1+acc else acc) 0

Is it now more usual to write is_vowel like I have done or as you have
done directly?

Quote:

> Here is my version of your code, where count_vowels uses foldl (of course,
> foldl is actually a recursive function, too, where tail recursion gets
> removed):

I read this in the Prelude, but are function like foldl just convinient
functions which can be used but shouldn't?

And last but not least. I wrote this Vowels in Haskell and Eiffel. If
s.o is interested here's the Eiffel-Code:

class VOWELS

creation make

feature
   make is
          local
                 counted_vowels: INTEGER;
                 str: STRING;
          do
                 print ("Please give me a string (exit for end): ");
                 io.read_line;
                 str := io.last_string;
                 if str.is_equal(exit_string) then
                        die_with_code(exit_success_code)
                 else
                        counted_vowels := count_vowels(str)
                        print ("The given string "); print (str);
                        print (" has "); print(counted_vowels); print (" vowels%N")
                        make
                 end;
          end; -- make

feature {NONE}

   exit_string: STRING is "exit";

   vowels : STRING is "AEIOUaeiou";

   is_vowel(ch: CHARACTER): BOOLEAN is
          do
                 if vowels.has(ch) then
                        Result := True;
                 end;
          ensure
                 Result implies vowels.has(ch)
          end;

   count_vowels(str: STRING): INTEGER is
          require
                 str_not_void: str /= Void;
          local
                 i: INTEGER;
          do
                 from i:=1;
                 invariant i >= 1 and then i <= str.count+1
                 variant str.count - i + 1
                 until i > str.count
                 loop
                        if is_vowel(str.item(i)) then
                           Result := Result + 1;
                        end -- if
                        i := i + 1;
                 end; -- from
          end

end -- class VOWELS

I think it's all usual Eiffel-Programming but the recursive call of
main. I rewrote it just for fun this way. But I think this example shows
niceley where the different weaknesses and strength of the Programming
Style are.

E.g the input/output is better done in an imperative style. And for this
in Haskell I think the >>=, >> and do were introduced.

And now to a fictious language (EPHIL) which combines the both
approaches:

Vowels
   -- Report the number of vowels in a given string
creation
   make
      do

                loop
                        print ("Please give me a string (exit for end): ");
                        io.read_line;
                        str := io.last_string;
                until   str.is_equal("exit")
                        print_line(["The given String: ",str, " has ",
                                                vowel_count(str), " vowels."])
                endloop
        end
private

   vowels : String is "aeiouAEIOU" end;

   vowel_count(s: String): Integer
      is
         if s.count = 0 then
            0
         elseif s.head.in(vowels) then
            1 + vowel_count(s.tail)
         else
            vowel_count(s.tail)
         endif
      ensure
         in_range: result >= 0 and result <= s.count
      end
endclass

It's up to you to judge the approaches. My opinion should be clear ;-)

Regards
Friedrich



Fri, 29 Jun 2001 03:00:00 GMT  
 OO and FP [Fwd: comparison OOP and FP (and how to put them together) was: Re: need help with haskell]
I take my bag of sins today. Sorry for reposting this and sorry for not
commenting why I've done that. I think it's to much burried in another
thread and maybe deserve it's own.

Regards
Friedrich



Fri, 29 Jun 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. comparison OOP and FP (and how to put them together) was: Re: need help with haskell

2. FP Component Libraries? (FP vs OOP)

3. OO and FP (was subtyping in FP)

4. DBC in functional languages [Was: comparison OOP and FP]

5. More than two FP-TB-3 with FP-TC-120

6. FP to FP Binary/Hex

7. FP in a larger scale (Re: Comparison of functional languages)

8. OOP => FP

9. FP in a larger scale (Re: Comparison of functional languages)

10. What language do you think is a good combination between OOP and FP

11. "FP+OOP=Haskell"

12. "FP+OOP=Haskell" paper

 

 
Powered by phpBB® Forum Software