caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: jonathan.roewen@gmail.com
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] HOFs, recursion, and being tail-rec...
Date: Mon, 13 Feb 2006 08:53:48 +0900 (JST)	[thread overview]
Message-ID: <20060213.085348.126575598.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <ad8cfe7e0602121408q15124406ta0e3a42a69c0abcc@mail.gmail.com>

From: Jonathan Roewen <jonathan.roewen@gmail.com>
> 
> 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).

By definition a depth-first-search cannot be tail-recursive: you need
a stack to implement the backtracking.
There is a degenerate case where all nodes are non-branching
(i.e. there is only one path), which in theory could be made
tail-recursive. But it would not be the case with your code, as
List.exists has no special case for the last element of the list (not
that it would make a lot of sense in general.)

> 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;;


  reply	other threads:[~2006-02-12 23:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-12 22:08 Jonathan Roewen
2006-02-12 23:53 ` Jacques Garrigue [this message]
2006-02-13  2:05   ` skaller
2006-02-13  2:47     ` Jonathan Roewen
2006-02-13  3:23       ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060213.085348.126575598.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=jonathan.roewen@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).