I'd probably prefer using a fold over explicit recursion. This gets easier if 
you split your code into two steps - one that does just one level of lookup, 
and one that does nested lookups. Additionally, I'd prefer list indexes rather 
than pair lookup functions:

(define (data/ref s key)
  (cond
    [(hash? s) (hash-ref s key)]
    [(list? s) (list-ref s key)]
    [else fail-somehow ...]))

(define (data/fetch s keys)
  (define (fetch-once key s)
    (data/ref s key))
  (foldl fetch-once s keys))


> (data/ref (hash 'a 1 'b 2) 'a)
1
> (data/ref '(a b c) 2)
'c
> (data/fetch (hash 'a '(foo bar baz) 'b 0) '(a 2))
'baz

By splitting the recursion (define (data/ref s key)
  (cond
    [(hash? s) (hash-ref s key)]
    [(list? s) (list-ref s key)]
    [else fail-somehow ...]))

(define (data/fetch s keys)
  (define (fetch-once key s)
    (data/ref s key))
  (foldl fetch-once s keys))


> (data/ref (hash 'a 1 'b 2) 'a)
1
> (data/ref '(a b c) 2)
'c
> (data/fetch (hash 'a '(foo bar baz) 'b 0) '(a 2))
'baz

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to