I have a problem, what happens is that I can't find a way to get all the data back from the list below:
(define lst1 (list
(list "p1" "clock" "1000" "10")
(list "p2" "shirt" "2000" "30")
(list "p3" "pants" "4000" "20")
(list "p4" "cap" "2300" "100")
(list "p5" "string" "1600" "25")
(list "p6" "glasses" "3000" "34")
(list "p7" "shoes" "400" "120")))
as a structure, the structure would be (make-product code product price quantity)
I tried but what it returns is only one data of the list but it does not return the whole list with the structure.
(define-struct product (code product price quantity))
(define(load-files lst)
(cond
[(empty? lst)empty]
[(list? lst)(list(make-product(first(first lst))(first(rest(first lst)))
(first(rest(first lst)))
(first(rest(rest(first lst))))))]
[else(load-files (rest lst))
]
)
)
You don't need to do an explicit recursion, this is a good opportunity to use higher-order functions like map
, curry
and apply
:
(define (load-files lst)
(map (curry apply make-product)
lst))
To make it a bit more explicit, the above is equivalent to:
(define (load-files lst)
(map (lambda (item)
(make-product (first item) (second item) (third item) (fourth item)))
lst))