caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] HOFs, recursion, and being tail-rec...
@ 2006-02-12 22:08 Jonathan Roewen
  2006-02-12 23:53 ` Jacques Garrigue
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Roewen @ 2006-02-12 22:08 UTC (permalink / raw)
  To: OCaml

Hi,

I have a simple implementation of depth-first-search, and was
wondering if my approach would qualify as tail-rec (whether from the
code it is/isn't, and whether ocaml can optimise it so it is).

val positions : 'a -> ('a * 'a) list -> 'a list -> 'a list
(* I think that's right type: returns positions we can traverse to,
omitting nodes we've previously visited *)

(* val dfs: 'a -> 'a -> ('a * 'a) list -> bool *)
let dfs start goal edges =
    let rec search visited position =
      if position = goal then true
      else List.exists (search (position::visited)) (positions
position edges (position::visited))
    in search [] start;;

Jonathan


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-02-13  3:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-12 22:08 [Caml-list] HOFs, recursion, and being tail-rec Jonathan Roewen
2006-02-12 23:53 ` Jacques Garrigue
2006-02-13  2:05   ` skaller
2006-02-13  2:47     ` Jonathan Roewen
2006-02-13  3:23       ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).