Forum Topic: Functional Programming

(185 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

RageOfOrder

Reply To Post Reply & Quote

Posted at: 10/6/09 01:56 PM

RageOfOrder EVIL LEVEL 09

Sign-Up: 08/30/02

Posts: 6,346

Quick. Someone give me the relevant code in their language of choice to print out the number from 1 to 10, inclusive.

For example, in Perl:

print for (1..10);

Now, in Functional Programming (FP) there is no such thing as a variable or a constant. [b]Everything[/b] is a function.
Here's how you print 1 through 10 in FP:

? print1to is (if = o [ id , {1} ] cons o [ {1} , {<>} ] rotl o cons o [ id , print1to o - o [ id , {1} ] ]).
Function print1to defined.

? print1to:10.
-> <1 2 3 4 5 6 7 8 9 10>

Stupid Languages & Concepts class.

At 9/28/09 06:57 PM, citricsquid wrote:
linux isn't for those who want windows. use windows if you want windows.

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 10/6/09 02:11 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

What is that code?

The pure function to make a range list is

range n m
  | n < m     = n : range (n+1) m
  | otherwise = [m]

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

kiwi-kiwi

Reply To Post Reply & Quote

Posted at: 10/6/09 02:23 PM

kiwi-kiwi LIGHT LEVEL 08

Sign-Up: 03/06/09

Posts: 655

At 10/6/09 01:56 PM, RageOfOrder wrote:
? print1to is (if = o [ id , {1} ] cons o [ {1} , {<>} ] rotl o cons o [ id , print1to o - o [ id , {1} ] ]).
Function print1to defined.

? print1to:10.
-> <1 2 3 4 5 6 7 8 9 10>

Scheme?

Also

for (int i=0;i<10;i++)
{
   printf("%d ",i+1);
}

None

gumOnShoe

Reply To Post Reply & Quote

Posted at: 10/6/09 04:17 PM

gumOnShoe LIGHT LEVEL 15

Sign-Up: 05/29/04

Posts: 14,107

mmmm scheme

(define (range num max)
(if (num < max)
(list n (range (+ num 1) max))
empty
)
)

(define (start max)
(range 0 max))
Returns a list from 0 to max. You can figure out how to print it yourself...

Mmmm parens. And hopefuly none of those words are stopwords/protected/etc...

FORUM MODERATOR PM Forum Abuse to: Me :: AIM: gumOnShoeNG
Improve Your Responses >:(

BBS Signature

None

RageOfOrder

Reply To Post Reply & Quote

Posted at: 10/7/09 03:01 PM

RageOfOrder EVIL LEVEL 09

Sign-Up: 08/30/02

Posts: 6,346

At 10/6/09 02:11 PM, GustTheASGuy wrote: What is that code?

A language called "FP", written by John Backus
Wikipedia

It was never designed for real-world use, just for education in schools.

At 9/28/09 06:57 PM, citricsquid wrote:
linux isn't for those who want windows. use windows if you want windows.

BBS Signature

None

CacheHelper

Reply To Post Reply & Quote

Posted at: 10/7/09 04:20 PM

CacheHelper NEUTRAL LEVEL 01

Sign-Up: 04/02/09

Posts: 486

PrintNumber(1, 10);

private void PrintNumber(int start, int stop)
{
     for (int i = start; i <= stop; i++)
     {
          Console.Write(i.ToString()); 
     }
}

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 10/8/09 03:43 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,418

That's not FP.

You just wanted to see some weird range implementations?

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Cinjection

Reply To Post Reply & Quote

Posted at: 10/12/09 02:26 PM

Cinjection LIGHT LEVEL 18

Sign-Up: 04/06/04

Posts: 2,490

Hehe. I approve :). I'm glad people are starting to care about functional programming. Making a list is one thing, but the really impressive stuff can be seen when you implement "data" structures using only functions.

Also,

(define (make-a-list final-len)
     (local ((define (make-list-internal final-len cnt-len)
                 (if (equal? final-len cnt-len)
                      empty
                      (cons (read)  (make-list-internal final-len (+ 1 cnt-len))))))

     (make-list-internal 10 0))

Makes a 10 element list with input from the user. :)

The best part about going on Newgrounds is that no one know that you're naked!
[My Coder Profile]

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 07:59 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!