Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006357OCamlOCaml generalpublic2014-03-28 16:322014-07-16 15:53
Reporterjohnwhitington 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusfeedbackResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0006357: Bind part of a while condition to a name
DescriptionWhen doing, for example, input with imperative constructs, there seem to be three ways to test the input for end of data as part of the conditional, whilst also using its value in the loop:

1)

let fin = ref false
  while !fin do
    match input_byte () with
      x when x = eod -> fin := true
    | x -> ...x...
  done

(pollutes scope with name 'fin', even though not relevant outside loop body)

2)

exception Finished

try
  while true do
    match input_byte () with
      x when x = eod -> raise Finished
    | x -> ...x...
  done
with
  Finished -> ......

(needs an exception - same one each time, or define a new one each time?)

3)

let x = ref 0 in
  while x := input_byte (); !x <> eod do
    ...!x...
  done

(pollutes scope with name 'x', even though not relevant outside loop body)


It would be nice to be able to bind part of the expression to a name, like this:

while input_byte () as x <> eod do
  ...x...
done

Now x is only available in the loop body, which is neater.

TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0011134)
frisch (developer)
2014-03-28 17:23

For complex loops and do-while-kind of conditions, I tend to merge the stop condition and the loop body, using the form <tt>while ... do () done</tt>.

In your case:
 

  while
     let x = input_byte () in
     x <> eod && begin
       ... x...;
       true
     end
  do () done

(0011135)
gasche (developer)
2014-03-28 17:48

Whenever control gets complex, I tend to revert to tail-recursive functions:

  let rec loop () =
    let x = input_byte () in
    if x <> eod then begin
      ...
      loop ()
    end
  in loop ()

I personally doubt the proposed change is worth a new language feature. Long-term I would be interested in syntactic sugar for iteration:

  for x in input_bytes stdin do
    ...
  done

(but I think for such a feature to be worthwile you need to support folding, monadic comprehensions and possibly generators/yield with a coherent syntax, which is easier said than done.)
(0011848)
doligez (administrator)
2014-07-16 15:52
edited on: 2014-07-16 15:53

FTR, I also use tail-recursive functions for all non-trivial loops.
Once you've got used to it, it's powerful and quite readable.


- Issue History
Date Modified Username Field Change
2014-03-28 16:32 johnwhitington New Issue
2014-03-28 17:23 frisch Note Added: 0011134
2014-03-28 17:48 gasche Note Added: 0011135
2014-07-16 15:52 doligez Note Added: 0011848
2014-07-16 15:53 doligez Note Edited: 0011848 View Revisions
2014-07-16 15:53 doligez Status new => feedback


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker