1. Functional programming

Hello everyone,

I've been playing with mLite, a lightweight interpreter for the ML functional programming language.

One of the benefits of learning another language is that it forces you to think differently. So I'm now re-examining my earlier RosettaCode submissions and future Euphoria submissions in the light of the functional approach. Someone's already done Happy Numbers in Euphoria, but this is how it could be done in mLite:

local 
   fun get_digits  
         (d, s) where (d = 0) = s 
      |    (d, s) = get_digits( d div 10, (d mod 10) :: s) 
      |    n = get_digits( n div 10, [n mod 10] ) 
   ; 
   fun mem (x, []) = false 
      |    (x, a :: as) where (x = a) = true 
      |    (x, _ :: as) = mem (x, as) 
in 
   fun happy 
         1 = "happy" 
      |   n = 
            let  
               val this = (fold (+,0) ` map (fn n = n ^ 2) ` get_digits n); 
               val sads = [2, 4, 16, 37, 58, 89, 145, 42, 20] 
            in 
               if (mem (n,sads)) then 
                  "unhappy" 
               else 
                  happy this 
            end 
end 
; 
 
map (fn n = (print n; print " is "; println ` happy n)) ` iota 2000; 

Kind regards
Bruce / bugmagnet

new topic     » topic index » view message » categorize

2. Re: Functional programming

Speaking for myself,
I still prefer Euphoria as its easier to read without the ";" statement end identifier thats also in PL/I and Pascal/Delphi if memory serves. Similar to the period "." in Cobol . Also the "`" character is easy to miss, and I have done exactly that in Bash scripts.
Hey, I'm 54 years old so my eyes are not what they used to be ;)
YMMV, but I'll stay with Euphoria for my general purpose programming needs thanks.

Alan

new topic     » goto parent     » topic index » view message » categorize

3. Re: Functional programming

Alan

I'm a year younger than you. And I'm not suggesting giving up Euphoria for mLite. Far from it in fact, as mLite is nowhere near as mature nor as generally powerful as Euphoria.

I've always wanted to learn how to program using the functional paradigm. Now I have a better idea how it works, and maybe it's helped to keep dementia at bay for a little longer.

Reminds me of Kenneth E. Iverson's paper "Notation as a Tool of Thought". mLite gives me another notation with which to think about programming problems.

Kind regards
bugmagnet

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu