Racket list in struct - list

I just started programming with Racket and now I have the following problem.
I have a struct with a list and I have to add up all prices in the list.
(define-struct item (name category price))
(define some-items
(list
(make-item "Book1" 'Book 40.97)
(make-item "Book2" 'Book 5.99)
(make-item "Book3" 'Book 20.60)
(make-item "Item" 'KitchenAccessory 2669.90)))
I know that I can return the price with: (item-price (first some-items)) or (item-price (car some-items)).
The problem is, that I dont know how I can add up all Items prices with this.
Answer to Óscar López:
May i filled the blanks not correctly, but Racket mark the code black when I press start and don't return anything.
(define (add-prices items)
(if (null? items)
0
(+ (first items)
(add-prices (rest items)))))

Short answer: traverse the list using recursion. This looks like homework, so I'll give you some hints; fill-in the blanks:
(define (add-prices items)
(if (null? items) ; if the list of items is empty
<???> ; what's the price of an empty list?
(+ <???> ; else add the price of the first item (*)
(add-prices <???>)))) ; with the prices of the rest of the list
(*) Notice that you already know how to write this part, simply get the price of the first item in the list using the appropriate procedures for accessing the value!
There are many ways to solve this problem. The one I'm proposing is the standard way to traverse a list, operating over each of the elements and recursively combining the results.

use foldl and map:
(foldl + 0
(map
(lambda (it)
(item-price it))
some-items))

Related

Returning smallest item in a list in Scheme

Note: I am doing this for homework. I'm not looking for the algorithm to solve my problem, just trying to understand how Scheme works.
I'm new to scheme and trying to write a small program to find the smallest item in a list. The program is working in the since that it is finding the correct answer (so the logic is somewhat sound), but I only know this because an error is coming up that it's trying to treat my answer as a function and call it.
(DEFINE (startmin mylist)
(
(repeatmin (CAR mylist) (CDR mylist))
))
(DEFINE (repeatmin curmin mylist)
(
(IF (NULL? mylist) ;If the list is empty
;This is where I'm at a loss. I want a way for this value to be
;sent out once the list is empty
curmin ;return the current minimum
(IF (< curmin (CAR mylist)) ;if the current minimum is less than the head of the list
(repeatmin curmin (CDR mylist)) ;recurse with curmin
(repeatmin (CAR mylist) (CDR mylist)) ;otherwise recurse with the head of the list.
)
)
))
I'm really at a loss of how I get the value, once found, back out of the recursion as it keeps trying to treat the value as a function.
Your parentheses are off. If you write
((if ...))
it means that the result from if is a function that immediately should be called once the value is cmoputed. It seems like you are using parentheses as if they are blocks, like {} in C, but they are not. In fact (begin ...) is a block in Scheme. And of course a function, let and cond terms have implicit begin.. Thus
(define (func . args)
(begin
x
y))
Is the same as
(define (func . args)
x
y)
Also please format your code correctly. Indentation helps you read the code and the nesting. I really don't pay attention to the parentheses, just the position which is crucial. Choose an editor that does this for you. Eg. DrRacket is a nice one I use.

Ignore/Delete Repeat Symbols in a Frequency List in Scheme [duplicate]

This is extremely easy if I can use an array in imperative language or map (tree-structure) in C++ for example. In scheme, I have no idea how to start this idea? Can anyone help me on this?
Thanks,
Your question wasn't very specific about what's being counted. I will presume you want to create some sort of frequency table of the elements. There are several ways to go about this. (If you're using Racket, scroll down to the bottom for my preferred solution.)
Portable, pure-functional, but verbose and slow
This approach uses an association list (alist) to hold the elements and their counts. For each item in the incoming list, it looks up the item in the alist, and increments the value of it exists, or initialises it to 1 if it doesn't.
(define (bagify lst)
(define (exclude alist key)
(fold (lambda (ass result)
(if (equal? (car ass) key)
result
(cons ass result)))
'() alist))
(fold (lambda (key bag)
(cond ((assoc key bag)
=> (lambda (old)
(let ((new (cons key (+ (cdr old) 1))))
(cons new (exclude bag key)))))
(else (let ((new (cons key 1)))
(cons new bag)))))
'() lst))
The incrementing is the interesting part. In order to be pure-functional, we can't actually change any element of the alist, but instead have to exclude the association being changed, then add that association (with the new value) to the result. For example, if you had the following alist:
((foo . 1) (bar . 2) (baz . 2))
and wanted to add 1 to baz's value, you create a new alist that excludes baz:
((foo . 1) (bar . 2))
then add baz's new value back on:
((baz . 3) (foo . 1) (bar . 2))
The second step is what the exclude function does, and is probably the most complicated part of the function.
Portable, succinct, fast, but non-functional
A much more straightforward way is to use a hash table (from SRFI 69), then update it piecemeal for each element of the list. Since we're updating the hash table directly, it's not pure-functional.
(define (bagify lst)
(let ((ht (make-hash-table)))
(define (process key)
(hash-table-update/default! ht key (lambda (x) (+ x 1)) 0))
(for-each process lst)
(hash-table->alist ht)))
Pure-functional, succinct, fast, but non-portable
This approach uses Racket-specific hash tables (which are different from SRFI 69's ones), which do support a pure-functional workflow. As another benefit, this version is also the most succinct of the three.
(define (bagify lst)
(foldl (lambda (key ht)
(hash-update ht key add1 0))
#hash() lst))
You can even use a for comprehension for this:
(define (bagify lst)
(for/fold ((ht #hash()))
((key (in-list lst)))
(hash-update ht key add1 0)))
This is more a sign of the shortcomings of the portable SRFI 69 hashing library, than any particular failing of Scheme for doing pure-functional tasks. With the right library, this task can be implemented easily and functionally.
In Racket, you could do
(count even? '(1 2 3 4))
But more seriously, doing this with lists in Scheme is much easier that what you mention. A list is either empty, or a pair holding the first item and the rest. Follow that definition in code and you'll get it to "write itself out".
Here's a hint for a start, based on HtDP (which is a good book to go through to learn about these things). Start with just the function "header" -- it should receive a predicate and a list:
(define (count what list)
...)
Add the types for the inputs -- what is some value, and list is a list of stuff:
;; count : Any List -> Int
(define (count what list)
...)
Now, given the type of list, and the definition of list as either an empty list or a pair of two things, we need to check which kind of list it is:
;; count : Any List -> Int
(define (count what list)
(cond [(null? list) ...]
[else ...]))
The first case should be obvious: how many what items are in the empty list?
For the second case, you know that it's a non-empty list, therefore you have two pieces of information: its head (which you get using first or car) and its tail (which you get with rest or cdr):
;; count : Any List -> Int
(define (count what list)
(cond [(null? list) ...]
[else ... (first list) ...
... (rest list) ...]))
All you need now is to figure out how to combine these two pieces of information to get the code. One last bit of information that makes it very straightforward is: since the tail of a (non-empty) list is itself a list, then you can use count to count stuff in it. Therefore, you can further conclude that you should use (count what (rest list)) in there.
In functional programming languages like Scheme you have to think a bit differently and exploit the way lists are being constructed. Instead of iterating over a list by incrementing an index, you go through the list recursively. You can remove the head of the list with car (single element), you can get the tail with cdr (a list itself) and you can glue together a head and its tail with cons. The outline of your function would be like this:
You have to "hand-down" the element you're searching for and the current count to each call of the function
If you hit the empty list, you're done with the list an you can output the result
If the car of the list equals the element you're looking for, call the function recursively with the cdr of the list and the counter + 1
If not, call the function recursively with the cdr of the list and the same counter value as before
In Scheme you generally use association lists as an O(n) poor-man's hashtable/dictionary. The only remaining issue for you would be how to update the associated element.

How to use recursion with elements in the list?

I'm trying to use recursion in the list and I need to go through all elements. Here is my code:
(define compare
(lambda (ls pred?)
(if (null? list)
#f
(pred? (list-ref ls (- (length ls) 2)) (list-ref ls (- (length ls) 1))))))
But it works only with last two elements. Result should be like this:
(compare '(1 2 3 4 5) <) -> #t
(compare '(1 2 8 4 5) <) -> #f
Do you have any idea what I should do?
You're not using recursion anywhere in the code. In fact, it has errors and I don't think you tested it thoroughly. For example:
The if condition should be (null? ls)
Using list-ref is not the way to go when traversing a list in Scheme, for that in general you want to use recursion, car, cdr, etc.
Again, where is the recursive call? compare should be called at some point!
I believe this is what you intended, it's not recursive but it's the simplest way to implement the procedure:
(define (compare ls pred?)
(apply pred? ls))
Because this looks like homework I can only give you some hints for solving the problem from scratch, without using apply. Fill-in the blanks:
(define (compare ls pred?)
(if <???> ; special case: if the list is empty
<???> ; then return true
(let loop ((prev <???>) ; general case, take 1st element
(ls <???>)) ; and take the rest of the list
(cond (<???> ; again: if the list is empty
<???>) ; then return true
(<???> ; if pred? is false for `prev` and current element
<???>) ; then return false
(else ; otherwise advance the recursion
(loop <???> <???>)))))) ; pass the new `prev` and the rest of the list
Notice that I used a named let for implementing the recursion, so loop is the recursive procedure here: you can see that loop is being called inside loop. Alternatively you could've defined a helper procedure. I had to do this for taking into account the special case where the list is initially empty.
The recursion works like this for the general case: two parameters are required, prev stores the previous element in the list and ls the rest of the list. at each point in the traversal we check to see if the predicates is false for the previous and the current element - if that is the case, then we return false. If not, we continue the recursion with a new prev (the current element) and the rest of the list. We keep going like this until the list is empty and only then we return true.

MIT Scheme Expression alternative form of append

I am currently attempting to resolve a question in a practice mid-term. The question asks for me to write an expression to append two lists (let us call them list1 and list2), and list2 must be appended to the end of list1. The function append cannot be used at any point in this. What I may use is cons, filter, accumulate, map, list-ref, and inumerate-interval. I have attempted various forms of getting to the solution, such as
(cons list1 list2)
(filter list? (map list (cons list1 list2)))
(list list1 list2)
(map list (list list1 list2))
I have spent 2 days attempting to find a solution to no avail. If anyone is able to guide me in the right direction, or even provide me with some form of assistance, I would be grateful.
Also, I apologize if there is some protocol that I am following incorrectly for the code formatting or the mannerisms in asking the question, as I am new to the site. Thank you.
Because this is homework, I can't give you a straight answer. Instead, I'll give you some hints, you can find the answer to your own question filing-in the blanks. This is the standard way to implement append:
(define (my-append l1 l2)
(cond (<???> ; if the first list is null
<???>) ; then return the second list
(<???> ; if the second list is null
<???>) ; then return the first list
(else ; otherwise `cons`
(cons <???> ; the first element of the first list
(my-append <???> l2))))) ; process the rest of the first list
The above solution uses cond, null?, cons, car and cdr. If you can't use any of those and you're restricted to the procedures in the question, try this instead (assuming accumulate is defined as a fold to the right):
(define (my-append l1 l2)
(accumulate
<???> ; what should be used for sticking list elements together?
<???> ; what should we return if the list being traversed is empty?
<???>)) ; this is the list that we want to traverse
The above solution only uses accumulate and cons, as requested in the question. The idea is: traverse the first list recreating it element by element, until the list is exhausted - at that point, the next element will be the second list.

Lisp, a couple of questions about lists and recursion

sorry to overflow with so many questions.
I have the following:
(defun recursive-function (string) "returns list of strings"
;I am trying to return flat list
; create list L
(append (mapcar 'recursive-function L)))
But since recursive-function returns a list, I end up with a list of a list of a list..., whereas I want just a flat list.
What is the proper way for me to implement recursion on functions which take a scalar and return a list of scalars?
Thanks.
If I understood correctly, you can combine reduce and append to flatten the list before returning it.
Example:
(reduce 'append '((1) (2) (3)))
Output:
(1 2 3)
In your case this might work:
(reduce 'append (mapcar 'recursive-function L))
I belive you are looking for mapcan:
[...] mapcan and mapcon are like mapcar and
maplist respectively, except that the
results of applying function are
combined into a list by the use of
nconc rather than list. [...]
(defun recursive-function (string) "returns list of strings"
;I am trying to return flat list
; create list L
(mapcan 'recursive-function L))