Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

How(Ways or approaches) to implement implementing lazy evaluation in the C (Imperative Programming)

23 views
Skip to first unread message

Chethan U

unread,
Sep 18, 2015, 10:31:40 AM9/18/15
to Bay Area Haskell Users Group
Hello Sir,

How to implement implementing lazy evaluation in the C (Imperative Programming)?

Lazy evaluation is one of the key features in functional programming languages such as Haskell. This feature allows the programmers to work on infinite series quite easily.  Functional programming languages work at higher abstraction focusing on what needs to be done instead of on how it can be done.

Imperative programming languages focus more on how a goal can be achieved and hence work at a lower level of abstraction.  Therefore, it is natural to ask how such a feature can be implemented in imperative programming languages such as C.


What are the Various Approaches of implementing lazy evaluation in the C programming language.

What are the
Constructs required for implementing lazy evaluation logic
 
‘C’ programming language support for the required constructs

Archontophoenix Quar

unread,
Sep 18, 2015, 6:03:21 PM9/18/15
to baha...@googlegroups.com
In languages where lazy evaluation is not built in, but that have a well-defined notion of function closure, like Scheme, you would typically represent an infinite stream of data with a function that returns a pair consisting of the next item in the stream and the function that represents the rest of the stream. The Streams section of the venerable Structure and Interpretation of Computer Programs (https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#%_sec_3.5) describes this technique.

C, however, does not have proper closures, so you would need to manage the state that a more sophisticated language would capture in a closure yourself. You can supply such a state to a function that computes a pair consisting of the next item in the stream and the next state -- in fact, just such a function forms the basis for the State Monad (https://wiki.haskell.org/State_Monad#Implementation).

Hope that helps.

A

--
You received this message because you are subscribed to the Google Groups "Bay Area Haskell Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bahaskell+...@googlegroups.com.
To post to this group, send email to baha...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bahaskell/9e7859ce-cbf4-4b12-a323-f27ef6911388%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Byron Hale

unread,
Sep 18, 2015, 6:12:47 PM9/18/15
to baha...@googlegroups.com
Some lazy evaluation is present in C, regarding && and ||.
Scheme has eager evaluation by default.  To get laziness, one needs to implement "freeze" and "thaw," or something similar.

Of course, that could make for a lot of extra calls to "freeze" or "thaw," etc.

Best regards,

Byron Hale


On Fri, Sep 18, 2015 at 7:31 AM, Chethan U <evolution...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages