Good language with which to learn FP? 
Author Message
 Good language with which to learn FP?

Hi there...  I am just getting beginning to look in to functional
programming, and I wonder if anyone could give me a recommendation
for a good language to start with.  Some feature I'd like to see:

 -- PD implementation on the Mac
 -- Easy to use input-and-output
 -- Free or cheap tutorial available

I have tried gofer and CAML light...  Both programs have good tutorial
information; however, I found the gofer manual to be a bit esoteric...
I have no experience in type theory, referential transparency, and other
topics discussed therein and in this newsgroup.  I found CAML light
easier to understand, but I find myself using its imperative-style
constructs (while statements and so forth) a bit much...  I wonder
if this is a bad way to learn FP, rather than using recursion and
pattern matching and other techniques.

Any feedback would be appreciated.  Thanks, -- Tim
--



Sat, 15 Feb 1997 02:02:10 GMT  
 Good language with which to learn FP?

Quote:

> Hi there...  I am just getting beginning to look in to functional
> programming, and I wonder if anyone could give me a recommendation
> for a good language to start with.  Some feature I'd like to see:
>  -- PD implementation on the Mac
>  -- Easy to use input-and-output
>  -- Free or cheap tutorial available
> I have tried gofer and CAML light...  Both programs have good tutorial
> information; however, I found the gofer manual to be a bit esoteric...

Although characteristic of the functional programming languages i've
seen, a relatively complicated type system is not inherent to the
functional style.  Try starting out with an untyped functional
language like Scheme (MacGambit for the Macintosh is available on
sumex and other archives).  You won't have to deal with any of the
esoteric stuff and has the added bonus of allowing conventional
inperative IO primitives.

rodrigo vanegas



Sat, 15 Feb 1997 05:26:43 GMT  
 Good language with which to learn FP?
: [I originally said]:
: > I wonder if anyone could give me a recommendation for a good language
: > to start with.  [ ... ]

: Although characteristic of the functional programming languages i've
: seen, a relatively complicated type system is not inherent to the
: functional style.  Try starting out with an untyped functional
: language like Scheme [ ... ].

As it happens, I know Common Lisp (fairly well), and I have used
scheme, and like it.  However, scheme doesn't strike me as having
the same ``flavor'' as some of the other FP languages I've seen and
read about...  I have a good handle on list processing and programs-
as-data from lisp, but these things don't seem to be as prominent
in CAML light or gofer (the two FPLs I have tried to learn so far).

Anyone care to comment on the differences and commonalities of
lisp/scheme and other FP languages?

Perhaps the thing I need to learn is the complex type system you
mentioned.

Thanks, -- Tim

(PS: please send any responses posted to me by email, as my newsfeed
 has been acting up recently.  Thanks.)
--



Wed, 19 Feb 1997 06:05:29 GMT  
 Good language with which to learn FP?

Quote:

> : Although characteristic of the functional programming languages i've
> : seen, a relatively complicated type system is not inherent to the
> : functional style.  Try starting out with an untyped functional
> : language like Scheme [ ... ].
> As it happens, I know Common Lisp (fairly well), and I have used
> scheme, and like it.  However, scheme doesn't strike me as having
> the same ``flavor'' as some of the other FP languages I've seen and
> read about...  I have a good handle on list processing and programs-
> as-data from lisp, but these things don't seem to be as prominent
> in CAML light or gofer (the two FPLs I have tried to learn so far).
> Anyone care to comment on the differences and commonalities of
> lisp/scheme and other FP languages?

As i understand it, higher-order function via lambda abstraction,
iteration via recursion, and no assignment are the defining features
of the functional style.  If you have written entire programs without
a single assignment or DO loop (which makes use of implicit
assignment) and taken advantage higher-order functions and recursion
to do everything you need, then you've written programs in a
functional style.

I see four main differences between lisp/scheme and other functional
languages like ML, Haskell, and FP.

First, the lisp/scheme type system is weak which means that types are
associated only with the data and not with the variables that refer to
the data.  The other languages do have typed formal parameters which
means that the functions are also typed.  

Second, although the functional style discourages assignment,
Lisp/Scheme does not prohibit it.  ML also allows it.

Third, languages like Hope and Haskell, incorporate some declarative
semantics implemented with pattern matching which Lisp/Scheme lacks.
While interesting and desirable, it's not really a "functional style"
criteria.  After all, it's just semantic sugar for a few IFs and CONDs
at the start of the equivalent Lisp/Scheme function.

Fourth, some functional languages like Haskell have lazy (non-strict)
evaluation semantics (check the FAQ for an explanation of what this
is).

But none of these differences should prevent you from writing pure
functional programs in Lisp/Scheme.

 1) you don't need strong typing.  though some prefer it.
 2) the language allows assignment.  just don't do it.
 3) you don't need pattern matching.  use CONDs.
 4) it doesn't do lazy evalution.  simulate with DELAY and FORCE.

rodrigo vanegas



Wed, 19 Feb 1997 15:23:04 GMT  
 Good language with which to learn FP?

   If you have written entire programs without
   a single assignment or DO loop (which makes use of implicit
                        ^^^^^^^^^^^                  ^^^^^^^^^^
   assignment) and [...],
  ^^^^^^^^^^^^
   then you've written programs in a functional style.

I agree with the remainder and the spirit of the article. But
it should be noted that - at least in Scheme - there is *no* problem with
DO-loops, because they are just syntactic sugar for the recursive (as
opposed to the iterative) formulation of the expression.

In Scheme:

        (do ((<v> <i> <r>) ...) (<t> <e1> <e2> ...) <c> ...)

is equivalent to

        (letrec ((f (lambda (<v> ...)
                        (if <t>
                            (begin <e1> <e2> ...)
                            (begin <c> ... (f <r> ...))))))
          (f <i> ...))

where ``f'' is some otherwise unused identifier.

The effect is that with

        (define l (do ((i 0 (+ i 1))
                       (x '() (cons (lambda () i) x)))
                      ((> i 10) x)))

the following behavior will be observed:

        ((car l))   --> 10
        ((cadr l))  --> 9
        ((caddr l)) --> 8
        ...

while with

        (define l*
          (let ((i 0)
                (x '()))
            (letrec ((loop (lambda ()
                             (if (> i 10) x
                                 (begin (set! x (cons (lambda () i) x))
                                        (set! i (+ i 1))
                                        (loop))))))
              (loop))))

we get

        ((car l*))   --> 11
        ((cadr l*))  --> 11
        ((caddr l*)) --> 11
        ...

--
-Matthias



Thu, 20 Feb 1997 00:22:07 GMT  
 Good language with which to learn FP?

Quote:

>    If you have written entire programs without
>    a single assignment or DO loop (which makes use of implicit
>                         ^^^^^^^^^^^                  ^^^^^^^^^^
>    assignment) and [...],
>   ^^^^^^^^^^^^
>    then you've written programs in a functional style.
> I agree with the remainder and the spirit of the article. But
> it should be noted that - at least in Scheme - there is *no* problem with
> DO-loops, because they are just syntactic sugar for the recursive (as
> opposed to the iterative) formulation of the expression.

[scheme code explaining sugar deleted]

Oh sure, i agree.  I would ammend my initial comment by saying that
even though DO loops can be implemented without assignment, using them
in ones code instead of recursion would not be the best way to learn
functional programming.

rodrigo vanegas



Fri, 21 Feb 1997 08:37:58 GMT  
 Good language with which to learn FP?
The issue is clouded by a confusion between functional programming and
fucntional languages.

Functional programming can be done in any language, functional languages
just make it easier, or force you to do it.

The features that most functional languages share have already been
covered, and I agree with them. My point is that you don't need fucntional
languages to do functional programming; it's just easier- see the loops
haskell has to jump though to simulate mutatable varables.

johan ovlinger



Tue, 25 Feb 1997 22:57:39 GMT  
 Good language with which to learn FP?

    Johan> The issue is clouded by a confusion between functional
    Johan> programming and fucntional languages.

    Johan> Functional programming can be done in any language,
    Johan> functional languages just make it easier, or force you to
    Johan> do it.

In an uninteresting sense Johan is perfectly right: a Turing machine
could be programmed to interpret any functional language, and every
programming language can emulate a Turing machine.

However, i think that the most important feature of FP is reification
of functions, ie the ability to deliver and *build* functional values.

Building functions is important. This is what lambda does. (built
functions are actually closures, with private data and common code;
another possibility whould be to incrementally generate code -and
closures won't be needed anymore).

Hence C is not functional. I never understand why C implementations
don't have any kind of lambda (or just a closure building
primitive). On modern Unix machines it could be rather easily
implemented by mallocating a small memory block containing closed data
and a dynamically generated stub to call a routine.

A minimal thing will be to have a builtin macro such as lambda(x,T,p)
--called with a value x, a type T, a function pointer p whose first
argument can be x and second argument has type T-- which returns a
function pointer to --in lisp notation--

(lambda (y:T) (p x y))  -- i want y to have T type.

For instance lambda("Hello", FILE*, fputs) would generate a stub whose
effect would be equivalent to the following routine

int fputs_Hello(FILE *f)
{
   return fputs("Hello", f);

Quote:
}

This could be done rather easily by mallocating a small block
containing a copy of x and a code stub which calls p with the first
argument and x (i assume that a mallocated block can contain code).

I don't thing that such a lambda would be harder to implement than C
variable arguments (which most of the time require a built in compiler
support). (i nearly believe that i could write such a lambda for
Sparcs with usual calling conventions in plain C).

It seems to me that closures are at least as useful in C as are
variable arguments.

--

Basile STARYNKEVITCH   ----  Commissariat a l Energie Atomique

--

Basile STARYNKEVITCH   ----  Commissariat a l Energie Atomique
DRN/DMT/SERMA * C.E. Saclay bat.470 * 91191 GIF/YVETTE CEDEX * France
fax: (33) 1- 69.08.23.81;    phone: (33) 1- 69.08.40.66

N.B. Any opinions expressed here are solely mine, and not of my organization.
N.B. Les opinions exprimees ici me sont personnelles et n engagent pas le CEA.

Please cite a small part of my mail in all answers
Veuillez citer une petite partie de mon courrier dans vos reponses



Tue, 25 Feb 1997 23:27:30 GMT  
 Good language with which to learn FP?

Quote:



>    Johan> Functional programming can be done in any language,
>    Johan> functional languages just make it easier, or force you to
>    Johan> do it.
>In an uninteresting sense Johan is perfectly right: a Turing machine
>could be programmed to interpret any functional language, and every
>programming language can emulate a Turing machine.

I don't think it's entirely uninteresting; I write C code in a fairly
functional style.  Usually it works well, though sometimes it blows up
on me (e.g., a macro processor that was organized around streams of
characters really should have used iterators instead -- though even there
a different data structure might have cured all the problems).

Quote:
> [...]
>Hence C is not functional. I never understand why C implementations
>don't have any kind of lambda (or just a closure building
>primitive). On modern Unix machines it could be rather easily
>implemented by mallocating a small memory block containing closed data
>and a dynamically generated stub to call a routine.

This would be considerably more useful if you provide a way for the
programmer to supply a destructor action, to be called when the closure
gets freed.  Without that you have something about equal in power to
Forth's CREATE...DOES> -- still very useful.  I actually considered
writing a C preprocessor for a similar language extension (C With
Closures, anyone?) just a few weeks ago, but then I came to my senses.


Thu, 27 Feb 1997 03:38:17 GMT  
 Good language with which to learn FP?
[Fergus Henderson]

|   Modern Unix machines do not make it as easy as you think.  Some
|   machines have separate instruction and data caches.  Generating code at
|   run-time and then executing it may require flushing the instruction
|   cache, which may be a very high-cost operation.  Even worse, on some
|   systems (e.g. DEC ULTRIX), only the super-user can flush the
|   instruction cache.  (Efficient implementations are possible, even on
|   such systems, but they're not quite that simple.)

I thought I knew Unix, but I keep learning new things all the time.  in
this case, it doesn't seem as likely to be true as many other things I have
learned recently.  how are function calls through function pointers done if
this is true?  it would seem that this requires flushing the instruction
cache, and be a high-cost operation, leading to the conclusion that some
relatively natural things to do in C and C++ should be avoided.  with C++'s
reliance on virtual function accessed through pointers in a table pointed
to by the object itself, I see it as unintelligent to build a new and fancy
machine that made this "a very high-cost operation".

#<Erik>
--
Microsoft is not the answer.  Microsoft is the question.  NO is the answer.



Fri, 28 Feb 1997 07:19:23 GMT  
 Good language with which to learn FP?
[Fergus Henderson]

|   Modern Unix machines do not make it as easy as you think.  Some
|   machines have separate instruction and data caches.  Generating code at
|   run-time and then executing it may require flushing the instruction
|   cache, which may be a very high-cost operation.  Even worse, on some
|   systems (e.g. DEC ULTRIX), only the super-user can flush the
|   instruction cache.  (Efficient implementations are possible, even on
|   such systems, but they're not quite that simple.)

I thought I knew Unix, but I keep learning new things all the time.  in
this case, it doesn't seem as likely to be true as many other things I have
learned recently.  how are function calls through function pointers done if
this is true?  it would seem that this requires flushing the instruction
cache, and be a high-cost operation, leading to the conclusion that some
relatively natural things to do in C and C++ should be avoided.  with C++'s
reliance on virtual function accessed through pointers in a table pointed
to by the object itself, I see it as unintelligent to build a new and fancy
machine that made this "a very high-cost operation".

#<Erik>
--
Microsoft is not the answer.  Microsoft is the question.  NO is the answer.



Fri, 28 Feb 1997 07:16:00 GMT  
 Good language with which to learn FP?
[Fergus Henderson]

|   Modern Unix machines do not make it as easy as you think.  Some
|   machines have separate instruction and data caches.  Generating code at
|   run-time and then executing it may require flushing the instruction
|   cache, which may be a very high-cost operation.  Even worse, on some
|   systems (e.g. DEC ULTRIX), only the super-user can flush the
|   instruction cache.  (Efficient implementations are possible, even on
|   such systems, but they're not quite that simple.)

I thought I knew Unix, but I keep learning new things all the time.  in
this case, it doesn't seem as likely to be true as many other things I have
learned recently.  how are function calls through function pointers done if
this is true?  it would seem that this requires flushing the instruction
cache, and be a high-cost operation, leading to the conclusion that some
relatively natural things to do in C and C++ should be avoided.  with C++'s
reliance on virtual function accessed through pointers in a table pointed
to by the object itself, I see it as unintelligent to build a new and fancy
machine that made this "a very high-cost operation".

#<Erik>
--
Microsoft is not the answer.  Microsoft is the question.  NO is the answer.



Fri, 28 Feb 1997 07:29:47 GMT  
 Good language with which to learn FP?

Quote:

>A minimal thing will be to have a builtin macro such as lambda(x,T,p)
>--called with a value x, a type T, a function pointer p whose first
>argument can be x and second argument has type T-- which returns a
>function pointer to --in lisp notation--
>(lambda (y:T) (p x y))  -- i want y to have T type.
>For instance lambda("Hello", FILE*, fputs) would generate a stub whose
>effect would be equivalent to the following routine
>int fputs_Hello(FILE *f)
>{
>   return fputs("Hello", f);
>}

Ok, I guess you can't do it without an explicit apply, but how about
the following?  You need to be very careful with casts...

---cut------cut------cut------cut------cut------cut------cut------cut---
#include <stdio.h>
/* Here is some code to represent lambda abstraction in C.
   The example code in the TESTING section may misbehave if
   sizeof(int) != sizeof(void *).


   I decline copyright.  This code is in the public domain.
*/

/* A lambda abstraction is a pointer to a function which wants two void *'s
   and returns a void *, along with the first argument already bound.  */
struct lambda_abstraction
{
        void *arg1;
        void * (*fptr)();

Quote:
};

/* Build a lambda abstraction.  The pointer returned has been
   malloc'd, so should eventually be free'd.  */

struct abstract_lambda *
lambda (void *arg1, void * (*fptr)())
{
        struct lambda_abstraction *tmp;

        if (NULL ==
            (tmp = (struct lambda_abstraction *)
             malloc(sizeof(struct lambda_abstraction)))) {
                return NULL;
        } else {
                tmp->arg1 = arg1;
                tmp->fptr = fptr;
                return tmp;
        }

Quote:
} /* lambda_abstraction * (*()) lambda() */

/* Apply a lambda abstraction to its argument.  Returns the result of */
/* the function.  */
void *
apply (struct lambda_abstraction *alambda, void *arg2)
{
        (*(alambda->fptr))(alambda->arg1, arg2);

Quote:
} /* int apply() */

#ifdef TESTING
void
main()
{
        struct lambda_abstraction *tmp;
        int fputs();

        tmp = lambda((void *)"Hello", (void * (*)()) fputs);

        printf("\nretval = %d\n", (int) apply(tmp, stdout));

        free(tmp);

Quote:
}

#endif /* TESTING */
---cut------cut------cut------cut------cut------cut------cut------cut---
--

   Maths Dept.; Univ. of Tasmania; GPO Box 252C; Hobart; Tasmania; 7001 AU

   GPL - "Just give source a chance."


Fri, 28 Feb 1997 09:05:32 GMT  
 Good language with which to learn FP?

Quote:

>I never understand why C implementations
>don't have any kind of lambda (or just a closure building
>primitive). On modern Unix machines it could be rather easily
>implemented by mallocating a small memory block containing closed data
>and a dynamically generated stub to call a routine.
[...]
>(i assume that a mallocated block can contain code).

Modern Unix machines do not make it as easy as you think.
Some machines have separate instruction and data caches.
Generating code at run-time and then executing it may require
flushing the instruction cache, which may be a very high-cost
operation.  Even worse, on some systems (e.g. DEC ULTRIX),
only the super-user can flush the instruction cache.
(Efficient implementations are possible, even on such systems,
but they're not quite that simple.)

--



Fri, 28 Feb 1997 04:00:20 GMT  
 Good language with which to learn FP?

   [Fergus Henderson]

   |   Modern Unix machines do not make it as easy as you think.  Some
   |   machines have separate instruction and data caches.  Generating code at
   |   run-time and then executing it may require flushing the instruction
   |   cache, which may be a very high-cost operation.  Even worse, on some
   |   systems (e.g. DEC ULTRIX), only the super-user can flush the
   |   instruction cache.  (Efficient implementations are possible, even on
   |   such systems, but they're not quite that simple.)

I thought the easy answer was to "pre-flush" a large area of memory, then
you can allocate code from there with no problems. I don't see any great
difficulties in doing that.

   I thought I knew Unix, but I keep learning new things all the time.  

It has nothing to do with Unix really, but with processor architecture
and/or implementation. The same problem arises, eg, with any machine
based on the 68030, which has (small) separate caches.

   in this case, it doesn't seem as likely to be true as many other things I have
   learned recently.

It is perfectly true. Self-modifying code is not well supported on recent
processors, and generating code requires a little care.

   how are function calls through function pointers done if
   this is true?  

Your function pointer is a data-value, and is fetched/stored with normal
load/store operations that go through the data-cache. No problem.

David Gay



Fri, 28 Feb 1997 16:18:14 GMT  
 
 [ 37 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. A good language to start fp

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

3. Best OO language to learn?

4. Best way to learn Assembly Language

5. Best Book to learn assembly language ?

6. Anyone know of a good book to learn Assembly Language Programming (x86/Pentium Processors)

7. best language to learn for a beginner

8. multi-language vs mono-language, 1/2 (was: Me and FP &c)

9. multi-language vs mono-language, 2/2 (was: Me and FP &c)

10. Speed of FP languages, prospects (was Re: Benchmarking Lazy Functional Languages)

11. Best FP primer book?

12. Good text book on FP ??

 

 
Powered by phpBB® Forum Software