xarxziux
2019-1-30 09:49:38

Hi. I’ve only recently learned Racket through the Exercism track and have just started on my first project. For this project, I need to be able to read a number of CSV files, combine them together in various ways, and output another CSV file with the results. To facilitate this, I want to create a function that convert a CSV file (which is read in as a Listof (Listof String) and convert it to a Listof (HashTable String String). That allows me to be able to access columns in the CSV file regardless of their position (which can vary between reports and even within versions of the same report.


xarxziux
2019-1-30 09:50:39

The problem is, using typed/racket, I can’t get this function to compile:

(: list->hash-simple ((Listof String) (Listof String) -> (HashTable String String)))
(define (list->hash-simple keys data)
  (for/hash
      ([k keys] [d data])
    (values k d)))

xarxziux
2019-1-30 09:51:39

I get this error message:

Type Checker: type mismatch
  expected: (U (Immutable-HashTable String String)
               (Mutable-HashTable String String)
               (Weak-HashTable String String))
  given: (Immutable-HashTable Any Any) in: (for/hash ((k keys) (d data)) (values k d))
. Type Checker: type mismatch
  expected: (U (Immutable-HashTable String String)
               (Mutable-HashTable String String)
               (Weak-HashTable String String))
  given: (U (Immutable-HashTable Any Any)
            (Mutable-HashTable Any Any)
            (Weak-HashTable Any Any)) in: (for/hash ((k keys) (d data)) (values k d))
. Type Checker: Summary: 2 errors encountered in:
  (for/hash ((k keys) (d data)) (values k d))
  (for/hash ((k keys) (d data)) (values k d))

xarxziux
2019-1-30 09:53:17

Typing the k and d values with ([k : String keys] [d : String data]) doesn’t change anything on this. What am I doing wrong here?


soegaard2
2019-1-30 11:09:47

@xarxziux Your solution looks fine. This variation works: (: list->hash-simple : (Listof String) (Listof String) -> (HashTable Any Any)) (define (list->hash-simple keys data) (for/hash ([k : String keys] [d : String data]) (values k d)))


soegaard2
2019-1-30 11:10:15

I am not sure why the output type (HashTable String String) causes problems.


xarxziux
2019-1-30 11:20:08

I went for HashTable String String as HashTable Any Any was causing problems elsewhere in the code. However I’m now getting the impression that HashTable typing is a little odd. For example, this code compiles:

(: read-hash ((Immutable-HashTable String String) String -> String))
(define (read-hash h k)
  (hash-ref h k))

But this doesn’t:

(: read-hash ((Immutable-HashTable String String) String -> String))
(define (read-hash h k)
  (hash-ref h k ""))

xarxziux
2019-1-30 11:21:24

I can get around this, but the work-arounds are complicated and verbose. I’d like to understand what the actual problem is.


soegaard2
2019-1-30 11:25:03

I see John recommends constructing a list first, and then make-immutable-hash. See the bottom of: https://www.brinckerhoff.org/clements/2178-csc430/Assignments/tr-notes.html#%28part._.Hash_.Table_.Construction%29


mark.warren
2019-1-30 11:29:03

Hi all, I’ve put my solution for Hunt the Wumpus on Rosetta Code if any wants a play. http://www.rosettacode.org/wiki/Hunt_The_Wumpus any suggestions for improvement are welcome.


soegaard2
2019-1-30 12:14:39

@mark.warren Maybe a comment before the definition of labyrinth-data explaining what the numbers mean?


mark.warren
2019-1-30 12:15:14

@soegaard2 Good idea


mark.warren
2019-1-30 13:47:53

@soegaard2 I’ve added some explanation for the labyrinth data.