Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Programming "only" in an environment of C and it's friends - Being a toolsmith

Reply
Thread Tools

Programming "only" in an environment of C and it's friends - Being a toolsmith

 
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2012
Ian Collins <(E-Mail Removed)> writes:
>Not all applications require speed.


Java seems to offer a good trade-off between the speed of writing code
and the speed of running code. One can code faster because of the GC and
other features, and at the same time it possibly is the fastest language
with a GC. About 32 times faster than Perl, but only 1.31 times slower
than C++, according to:

http://shootout.alioth.debian.org/u3...re-fastest.php
http://shootout.alioth.debian.org/u64q/java.php

 
Reply With Quote
 
 
 
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      05-28-2012
On Monday, May 28, 2012 12:47:26 PM UTC+1, Stefan Ram wrote:

<snip>

> When one does some »AI-style« programming with a graph where
> multiple objects link to each other in a very dynamic way,
> memory management can become hard, so this is also easier to
> code in GC languages.
>
> Expressions in functional languages often involve transfer of
> ownership or sharing of ownership of run-time allocated objects
> and can't really be written in C.


why not? really, why not? I'm actually trying to implement one such languages and I don't see why C makes it necessarily hard to do

> Apart from some simple cases,
> one cannot write in a pure functional style in C.


why not?

> One can »emulate« OOP in C, but it sometimes needs some more
> complicated wording.


encapsulation, easy. polymorphism is a bit harder. You can do some nasty things with structs but I'm not sure it's worth it. Could you do some sort ofDuck Typing?

> The need for manual memory management can
> become distracting.


yes, but I'm so used to it C its just sort of there in the background.

> So, C is a »multi-paradigm« language only to some extend.


I'm not convinced. You're back to Turing Completness. I'm sure I could write a functional program in COBOL- but would I enjoy it?

 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      05-28-2012
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Monday, May 28, 2012 12:47:26 PM UTC+1, Stefan Ram wrote:
>
> <snip>
>
>> When one does some »AI-style« programming with a graph where
>> multiple objects link to each other in a very dynamic way,
>> memory management can become hard, so this is also easier to
>> code in GC languages.
>>
>> Expressions in functional languages often involve transfer of
>> ownership or sharing of ownership of run-time allocated objects
>> and can't really be written in C.

>
> why not? really, why not? I'm actually trying to implement one such
> languages and I don't see why C makes it necessarily hard to do


Maybe the difference is between writing such code directly in C, and
implementing such code in another language, in C.

--
Bartc



 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>why not? really, why not? I'm actually trying to implement
>one such languages and I don't see why C makes it necessarily
>hard to do


I was not talking about using C to /implement/ a pure
functional language (PFL), but about using C to write in a
pure functional manner in C itself (i.e., using C as a PFL).

>>Apart from some simple cases,
>>one cannot write in a pure functional style in C.

>why not?


In »print(eval(input()))«, eval() is supposed to return a
meaningful value, which in a PFL has to have the type of the
image of the function »eval«. In all but the most simple
cases, such a value is a dynamically allocated graph.

When we write this in C: How should »eval« behave, when
a called »malloc« returns 0? The usual good practice in C
is to report such a failure to the caller. But the return
value of »eval« already has another meaning.

Moreover, when should the graph allocated by »eval« be
deallocated again? In »print«? But what should one then
call the next time one wants to print a value /not/
deallocating it?

So the need to check allocation attempts and to manage
dynamic memory in C makes such wording as
»print(eval(input()))« difficult in C.

Also, in C, blocks are not functions, so one lacks
function literals (anonymous functions) in C and
one also lacks lazy evaluation of arguments (see below).

>>One can »emulate« OOP in C, but it sometimes needs some more
>>complicated wording.

>encapsulation, easy. polymorphism is a bit harder. You can do
>some nasty things with structs but I'm not sure it's worth
>it. Could you do some sort of Duck Typing?


You can do everything you can imagine, but when using it
in C itself, the wording might be more complicated. In a
pure object-oriented language (POOL), even blocks are
objects, so, in Smalltalk, one can implement a custom if
statement with a custom boolean type that can be used as

a > 0 ifTrue: [a := 0]

. This will set »a := 0« if a > 0. In C, one cannot write

( greater( a, 0 )->ifTrue )({ a = 0; })

since blocks are not expressions, one would need to write

( greater( a, 0 )->ifTrue )( set( a, 0 ))

using a function call. But since, in C, arguments are
evaluated always, this would always set a to 0. We don't
have lazy evaluation in C for function arguments. This
also applys to the question of writing in a PFL style
in C. (PFLs and POOLs have some thinks in common.)

Moreover, »greater( a, 0 )« was assumed to return a
structure with a function pointer »ifTrue«, that needs to be
allocated dynamically. This also raises the question how it
should report a failure due to lack of memory and how that
memory should be managed after a successful allocation.

 
Reply With Quote
 
nick_keighley_nospam@hotmail.com
Guest
Posts: n/a
 
      05-28-2012
On Monday, May 28, 2012 2:04:48 PM UTC+1, Stefan Ram wrote:
> (E-Mail Removed) writes:
> >why not? really, why not? I'm actually trying to implement
> >one such languages and I don't see why C makes it necessarily
> >hard to do

>
> I was not talking about using C to /implement/ a pure
> functional language (PFL), but about using C to write in a
> pure functional manner in C itself (i.e., using C as a PFL).
>
> >>Apart from some simple cases,
> >>one cannot write in a pure functional style in C.

> >why not?

>
> In »print(eval(input()))«, eval() is supposed to return a
> meaningful value, which in a PFL has to have the type of the
> image of the function »eval«. In all but the most simple
> cases, such a value is a dynamically allocated graph.


not all functional languages are dynamically typed

> When we write this in C: How should »eval« behave, when
> a called »malloc« returns 0? The usual good practice in C
> is to report such a failure to the caller. But the return
> value of »eval« already has another meaning.
>
> Moreover, when should the graph allocated by »eval« be
> deallocated again? In »print«? But what should one then
> call the next time one wants to print a value /not/
> deallocating it?
>
> So the need to check allocation attempts and to manage
> dynamic memory in C makes such wording as
> »print(eval(input()))« difficult in C.
>
> Also, in C, blocks are not functions, so one lacks
> function literals (anonymous functions) in C and
> one also lacks lazy evaluation of arguments (see below).
>
> >>One can »emulate« OOP in C, but it sometimes needs some more
> >>complicated wording.

> >encapsulation, easy. polymorphism is a bit harder. You can do
> >some nasty things with structs but I'm not sure it's worth
> >it. Could you do some sort of Duck Typing?

>
> You can do everything you can imagine, but when using it
> in C itself, the wording might be more complicated. In a
> pure object-oriented language (POOL), even blocks are
> objects, so, in Smalltalk, one can implement a custom if
> statement with a custom boolean type that can be used as
>
> a > 0 ifTrue: [a := 0]
>
> . This will set »a := 0« if a > 0. In C, one cannot write
>
> ( greater( a, 0 )->ifTrue )({ a = 0; })
>
> since blocks are not expressions, one would need to write
>
> ( greater( a, 0 )->ifTrue )( set( a, 0 ))
>
> using a function call. But since, in C, arguments are
> evaluated always, this would always set a to 0. We don't
> have lazy evaluation in C for function arguments. This
> also applys to the question of writing in a PFL style
> in C. (PFLs and POOLs have some thinks in common.)
>
> Moreover, »greater( a, 0 )« was assumed to return a
> structure with a function pointer »ifTrue«, that needs to be
> allocated dynamically. This also raises the question how it
> should report a failure due to lack of memory and how that
> memory should be managed after a successful allocation.


thanks!
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-28-2012
Stefan Ram wrote:

> Java seems to offer a good trade-off between the speed of writing code
> and the speed of running code. One can code faster because of the GC and
> other features, and at the same time it possibly is the fastest language
> with a GC. About 32 times faster than Perl, but only 1.31 times slower
> than C++, according to:
>
> http://shootout.alioth.debian.org/u3...languages-are-

fastest.php
> http://shootout.alioth.debian.org/u64q/java.php


I would argue that Java's problem isn't necessarily how slow it might be on
some benchmark, but how responsive Java programs might appear to the user.
I've noticed that, when compared to some GUI applications developed in, say,
C++, Java apps present a noticeable lag. This tends to degrade the user
experience.

Granted, this might not have anything to do with the Java language and
everything to do how specific programs are developed.


Rui Maciel
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      05-28-2012
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> (E-Mail Removed) writes:


> You can do everything you can imagine, but when using it
> in C itself, the wording might be more complicated. In a
> pure object-oriented language (POOL), even blocks are
> objects, so, in Smalltalk, one can implement a custom if
> statement with a custom boolean type that can be used as
>
> a > 0 ifTrue: [a := 0]
>
> . This will set »a := 0« if a > 0.


In other words, if (a>0) a=0;

> In C, one cannot write
>
> ( greater( a, 0 )->ifTrue )({ a = 0; })
>
> since blocks are not expressions, one would need to write
>
> ( greater( a, 0 )->ifTrue )( set( a, 0 ))


Perhaps you need a more elaborate example where there isn't a trivial way of
doing it in C...

> using a function call. But since, in C, arguments are
> evaluated always, this would always set a to 0. We don't
> have lazy evaluation in C for function arguments.


C has function pointers. Those are only evaluated on demand. Of course, any
parameters have to be supplied separately, and the body of the function that
is pointed to - the code to be executed on demand- has to be written
elsewhere.

So it's somewhat untidy and makes it harder to follow. But then, I can
imagine that code full of conditional block objects that may or may not have
been executed yet, sounds pretty complicated too.

> Moreover, »greater( a, 0 )« was assumed to return a
> structure with a function pointer »ifTrue«, that needs to be
> allocated dynamically. This also raises the question how it
> should report a failure due to lack of memory and how that
> memory should be managed after a successful allocation.


I can't see that being a real issue. If this is is supposed to be a higher
level language, that you don't really want to be thinking about that stuff
at this level. It needs to be taken care of elsewhere. Otherwise the code is
going to be impossible and lose any advantage of the functional style.

(As a matter of interest, what does Smalltalk do when the program runs out
of memory for objects? If errors can be trapped by the user program, how
useful is that inside a deeply nested piece of code?)

--
Bartc



 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      05-28-2012
בתאריך יום ש×*י,28 במאי 2012 15:25:49 UTC+1, מאת Bart:
>
> Perhaps you need a more elaborate example where there isn't a trivial wayof
> doing it in C...
>

Write a function which takes a function foo() as a parameter, and as an output returns a function which is identical to foo(), except where foo() produces nan on divide by zero, creates a very high value instead.



 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-28-2012
"BartC" <(E-Mail Removed)> writes:
>I can't see that being a real issue. If this is is supposed to be a higher
>level language, that you don't really want to be thinking about that stuff
>at this level. It needs to be taken care of elsewhere. Otherwise the code is
>going to be impossible and lose any advantage of the functional style.


Therefore, the host language should have a GC.

(I forgot to mention that in

print(eval(input()))

, »print« just can't deallocate the graph obtained from »eval«,
since it does not know whether parts of that graph are shared with other
owners. So some kind of GC or global memory management is necessaary.)

>(As a matter of interest, what does Smalltalk do when the program runs out
>of memory for objects? If errors can be trapped by the user program, how
>useful is that inside a deeply nested piece of code?)


I don't know. Maybe execution is based on the assumption that this will
always work, and is interrupted or aborted otherwise.

In Java, a run-time error is thrown that can be caught and handled.
This sounds hard, but I used this once: The handler first released some
memory set aside for this case and then was able to handle the situation
using this memory.

In C, the classical high-quality coding style is to check the result
of every malloc, fopen, ... and to act accordingly. However, sometimes
it already solves a problem at hand to assume success of all such calls
and not even explicitly free() allocated memory, again.

Maybe, one /can/ check too much, e.g.,

if( printf( "abc\n" )!= 4 )
{ if( fprintf( stderr, "printf failed!\n" )!= 15 )
{ /* now, what? ...

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      05-28-2012


"Malcolm McLean" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> בתאריך יום ש×*י, 28 במאי 2012 15:25:49 UTC+1, מאת Bart:
>>
>> Perhaps you need a more elaborate example where there isn't a trivial way
>> of
>> doing it in C...
>>

> Write a function which takes a function foo() as a parameter, and as an
> output returns a function which is identical to foo(), except where foo()
> produces nan on divide by zero, creates a very high value instead.


That sounds near impossible, in whatever language. Assuming you aren't
talking about a foo() merely returning a different floating point result,
but one that may or may not include floating point operations, and if it
does, then to alter the nature of all floating point operations executed
while in the lexical scope of foo()'s replacement.

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Friends Don’t Let Friends Do IE6 Lawrence D'Oliveiro NZ Computing 33 02-11-2010 10:14 AM
hello friends...wanna make friends visit nicki C Programming 0 10-13-2008 11:51 AM
MEET UR SCHOOL & COLLEGE FRIENDS. UR FRIENDS ARE WAITING FOR U.. sai.sri206@gmail.com C++ 0 10-28-2007 08:43 PM
Friends don't let friends drink and fly through space =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-29-2007 03:52 AM
member functions as friends - friends of each other? bipod.rafique@gmail.com C++ 2 07-16-2005 10:55 AM



Advertisments