Help in understanding 'letrec' example

52 views
Skip to first unread message

Utkarsh Singh

unread,
May 8, 2021, 3:50:09 AM5/8/21
to us...@racket-lang.org
Hi,

First of all I would like to thank Racket community for creating and
maintaining top quality documentation at https://docs.racket-lang.org/
and even providing a local copy for it.

Currently I am having some difficulties in understanding this letrec
example from Racket Guide docs
(https://docs.racket-lang.org/guide/let.html#%28part._.Recursive_.Binding__letrec%29):

(letrec ([tarzan-near-top-of-tree?
(lambda (name path depth)
(or (equal? name "tarzan")
(and (directory-exists? path)
(tarzan-in-directory? path depth))))]
[tarzan-in-directory?
(lambda (dir depth)
(cond
[(zero? depth) #f]
[else
(ormap
(λ (elem)
(tarzan-near-top-of-tree? (path-element->string elem)
(build-path dir elem)
(- depth 1)))
(directory-list dir))]))])
(tarzan-near-top-of-tree? "tmp"
(find-system-path 'temp-dir)
4))

Problem:
I having some problem on how recursion is working here and what is the
problem we are solving here. Are we finding a file with (name?
"tarzan") or something else?

--
Utkarsh Singh
http://utkarshsingh.xyz

kalime...@gmail.com

unread,
May 9, 2021, 2:33:16 AM5/9/21
to Racket Users
We are finding a file (or directory) with name "tarzan" inside all directories inside given path upto given depth.
Recursion is needed here, because  tarzan-near-top-of-tree? calls  tarzan-in-directory? and  tarzan-in-directory? calls  tarzan-near-top-of-tree? for each file in given directory.

суббота, 8 мая 2021 г. в 12:50:09 UTC+5, Utkarsh Singh:

Utkarsh Singh

unread,
May 9, 2021, 6:06:35 AM5/9/21
to us...@racket-lang.org
Hi,

> We are finding a file (or directory) with name "tarzan" inside all
> directories inside given path upto given depth.
>
> Recursion is needed here, because tarzan-near-top-of-tree? calls
> tarzan-in-directory? and tarzan-in-directory? calls
> tarzan-near-top-of-tree? for each file in given directory.

Thanks! I finally understood it.

Here is the explanation (for reference):

Consider a sample directory called test with following structure (ASCII
art using 'tree' command):

test
├── dir1
│   └── subdir1
│   └── file1
└── dir2

Our code will first recursively search for all file in dir1 upto depth 4
and then move to dir2. Using ormap insures that search for 'tarzan'
will stop at first match (or first true result).

David Storrs

unread,
May 13, 2021, 1:13:16 PM5/13/21
to Utkarsh Singh, Racket Users
Incidentally, a more concise way of doing this would be:

(define target (build-path "tarzan")) ; convert to path only once
(for/or ([item (in-directory "/tmp/test")]) ; or whatever directory you want to start in
  (equal? target (file-name-from-path item)))

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/87h7jdslzh.fsf%40gmail.com.

Utkarsh Singh

unread,
May 14, 2021, 10:34:46 AM5/14/21
to David Storrs, Racket Users
Hi David,

On 2021-05-13, 13:12 -0400, David Storrs <david....@gmail.com> wrote:

> Incidentally, a more concise way of doing this would be:
>
> (define target (build-path "tarzan")) ; convert to path only once
> (for/or ([item (in-directory "/tmp/test")]) ; or whatever directory
> you want to start in
> (equal? target (file-name-from-path item)))

Thanks! This looks good.
Reply all
Reply to author
Forward
0 new messages