This question already has answers here:
Upside down text
(2 answers)
Closed 10 years ago.
I got the letters to turn upside down. p to d What I can't seem to do is get a string of words to turn upside down. Like house upside down to ǝsnoɥ. any help would be appreciated. I am just about done. I am using beginning student with list abbreviations.
#;(first (first l2))
#;(first (rest l2))
#;(first (rest (first l2)))
(define (helper character list)
(cond [(string=? character (first (first list))) (first (rest (first list)))]
[else (helper character (rest list))]))
(check-expect (helper "p" table-1) "d")
(check-expect (helper "t" table-1) "ʇ")
;;_______________________________________________________________________________
(define (upside-down string)
(upside-down "cat")
A couple of comments on your helper procedure:
You should add an extra condition for the case when character is not in the list - if that happens, simply return the same character. Otherwise, an error will occur
The parameter should not be named list, that's a built-in procedure and it's a bad practice to overwrite it
Test it throughly before proceeding.
Once that's sorted out, here's how you could implement upside-down, fill-in the blanks:
(define (upside-down str)
(<???> ; finally, do the opposite of explode on the list
(map (lambda (c) ; map over each of the characters
(<???> <???> <???>)) ; call helper on the character
(<???> (<???> str))))) ; reverse the exploded string
If you're not allowed to use map or lambda, then implement a convert procedure that iterates over each of the characters in the string and applies helper on each one, returning a list with the result of applying it - basically, implement you own map that always applies helper to each element in the input list:
(define (convert lst)
<???>)
(convert '("c" "a" "t"))
=> '("ɔ" "ɐ" "ʇ")
Now we can write upside-down using convert:
(define (upside-down str)
(<???> ; finally, do the opposite of explode on the list
(convert ; use our shiny new procedure
(<???> (<???> str))))) ; reverse the exploded string
Related
This is a homework question
The function takes in a list as the parameter, which may contain as many layers as sublists as needed For example, '(a (1 b 3)) or '((a 3 5) (b (3 1) 4)). The output has the same list structure of the input (meaning that sublists are maintained), but the car of each list is the sum of all numbers in the list. And all other non-numeric values are discarded. As an example output, consider '((a 3 5) (b (3 1) 4)), the output should be '(16 (8) (8 (4))). Also, only use basic scheme instructions/operations such as + - * /, car, cdr, cons, append, null?, number?, if/else, cond, etc.. Cannot Use a helper method.
So far this is the code I have, which sometimes partially does the job. But I'm having a really hard time figuring out how to get the sum from the sublists to add up at one spot at the car of the outmost list.
(define partialsums*
(lambda (lis)
(cond
[(null? lis) '(0)]
[(list? (car lis)) (cons (partialsums* (car lis)) (if (not (null? (cdr lis))) (partialsums* (cdr lis)) '()))]
[(number? (car lis)) (cons (+ (car lis) (car (partialsums* (cdr lis)))) '())]
[else (cons (+ 0 (car (partialsums* (cdr lis)))) '())])))
I've already spent several hours on this but couldn't quite grasp how to correctly approach the problem, probably because this is my first week using scheme :(. Any help is appreciated.
Also, I cannot use a helper method. Everything needs to be done inside one function in a recursive style. letrec is not allowed either.
To make life easy, you should model the data. Since there are no types, we can do this informally.
What is the structure of the input?
We can model it like "Data Definitions" from How to Design Programs. Read the "Intertwined Data" section because our data definition is similar to that of an S-expression.
; A NestedElem is one of:
; - Atom
; - NestedList
; An Atom is one of:
; - Number
; - Symbol
; A NestedList is one of
; - '()
; - (cons NestedElem NestedList)
We can define an atom? predicate to help us differentiate between clauses of the kinds of data in our program.
; Any -> Boolean
; is `a` an atom?
(define atom?
(lambda (a)
(or (number? a)
(symbol? a))))
The structure of the program should match the structure of the Data.
So we define a "template" on our data. It distinguished and destructs each data into clauses. It further de-structures the rhs of a clause.
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) ...]
[else ...])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (car nl)... (cdr nl))])))
We definitely know more about the data. (car nl) in the nested-list-template is of type NestedElem. Therefore we can fill up some ...s with calls to templates that deal with that kind of data. In the same vein, we can wrap recursive calls around expressions of a datatype we know of.
; NestedElem -> ...
(define nested-elem-template
(lambda (ne)
(cond
[(atom? ne) (atom-template ne)]
[else (nested-list-template ne)])))
; Atom -> ...
(define atom-template
(lambda (atom)
(cond [(number? atom) ...]
[(symbol? atom) ...])))
; NestedList -> ...
(define nested-list-template
(lambda (nl)
(cond [(null? nl) ...]
[else (... (nested-elem-template (car nl))
... (nested-list-template (cdr nl)))])))
Now we can "fill in the blanks".
We can "filter", "map", and "fold" over this data structure. All those can be defined using the template as a scaffold.
Note 1: Your HW asks you to do multiple tasks:
remove the symbols
sum up the numbers
cons the sum onto every list
Don't try to do everything in a single function. Delegate into multiple helper functions/traversals.
Note 2: I did not model the output type. It's the same as input type except that Symbol is no longer an atom.
Write a Scheme function that takes two atoms and a list as parameters and returns a list identical to the parameter list except all occurrences of the first given atom in the list are replaced with the second given atom.
(define(swap val1 val2 lst)
(cond ((null? lst) (lst val1))
((equal? val2 lst) (lst))
(else(equal? val2 (cadr lst)) (swap val1 val2 (cadr lst)))))
Two major syntactical problems is that (lst val1) and (lst) tries to call lst as a function, and that else shouldn't have a condition - the cond form is
(cond (condition1 expression1)
(condition2 expression2)
(else expression))
(equal? val2 lst) says "val2 is equal to the entire list lst". This does not make sense.
(equal? val2 (cadr lst)) says "val2 is equal to the second element of the list lst". You don't need to care about the second element.
The first element is car. The tail is cdr. (Many use first and rest, which are more modern names.)
You're pretty close to a solution, so I'll just give you a structure:
If the list is empty, the result is the empty list.
If the first element of the list is the one you're replacing, cons the new value onto the result of recursing on the rest of the list.
Otherwise, cons the existing value onto the result of recursing on the rest of the list.
This must be very easy to accomplish but I am new to racket and dont know how:
I have a list (1 2 3 4) and would like to convert it into (1)(2)(3)(4)
Or is there a way to build it as (1)(2)(3)(4). I am using
cons '(element) call-function
to build it inside a function (recursively)
Try this:
(map list '(1 2 3 4))
From your text, I see that you do '(element). Problem with that is that everything which is quoted is never anything but what you see. Thus if element happens to be a variable it won't be expanded because of the quote.
The right way to get a list with one element would be to use list. eg. (list element) to get whatever the variable element to be the one element in your list. However, you won't need this in a roll-your-own recursive procedure:
(define (listify lst)
(if (null? lst) ; if lst is null we are done
'() ; evaluate to the empty list
(cons (list (car lst)) ; else we make a list with the first element
(listify (cdr lst))))) ; and listify the rest of the list too
Most of the procedure now is facilitating going through the argument, but since it's a common thing to do we can use higher order procedures with foldr so that you only concentrating on what is going to happen with the element in this chain in correspondence with the rest of the process:
(define (listify lst)
(foldr (lambda (e acc)
(cons (list e) ; chain this element wrapped in a list
acc)) ; with the result from the rest of the list
'() ; initiate with an empty list
lst)) ; go through lst
Of course, since we do something with each element in a list and nothing fancy by using map we only need to supply what to do with each element rather telling how to join the chains in the list together as well.
(define (listify lst)
(map list lst)) ; make a new list by applying a list of each element
It's actually a single argument version of zip:
(require srfi/1)
(zip '(1 2 3 4)) ; ==> ((1) (2) (3) (4))
(zip '(1 2 3) '(a b c)) ; ==> ((1 a) (2 b) (3 c))
There you go. As simple as it can get.
I need to write a good set function that checks whether its argument lst is a properly represented set, i.e. it is a list consisting only of integers, with no duplicates, and returns true #t or false #f. For example:
(good-set? (1 5 2)) => #t
(good-set? ()) => #t
(good-set? (1 5 5)) => #f
(good-set? (1 (5) 2)) => #f
so I have began writing the function as:
(define (good-set? lst)
so I don't know how to proceed after this. Can anybody help?
One option would be to use andmap and sets, as has been suggested by #soegaard:
(define (good-set? lst) ; it's a good set if:
(and (andmap integer? lst) ; all its elements are integers and
(= (length lst) ; the list's length equals the size
(set-count (list->set lst))))) ; of a set with the same elements
But if you can't use sets or other advanced procedures, then traverse the list and test if the current element is an integer and is not present somewhere else in the list (use member for this), repeating this test for each element until there are no more elements in the list. Here's the general idea, fill-in the blanks:
(define (good-set? lst)
(cond (<???> ; if the list is empty
<???>) ; then it's a good set
((or <???> ; if the 1st element is not an integer or
<???>) ; the 1st element is in the rest of the list
<???>) ; then it's NOT a good set
(else ; otherwise
(good-set? <???>)))) ; advance recursion
Sets are built into the Racket standard library: I would recommend not reimplementing them in terms of lists unless you really need to do something customized.
If we need to treat this as a homework assignment, I would recommend using a design methodology to systematically attack this problem. In this case, see something like How to Design Programs with regards to designing functions that work on lists. As a brief sketch, we'd systematically figure out:
What's the structure of the data I'm working with?
What tests cases do I consider? (including the base case)
What's the overall shape of the function?
What's the meaning of the natural recursion?
How do I combine the result of the natural recursion in order to compute a solution to the total?
For this, check if the first number is duplicated, if it is not, then recurse by checking the rest. As such:
(define (good-set? list)
(or (null? list) ; nothing left, good!
(let ((head (car list)))
(rest (cdr list)))
(and (number? head) ; a number
(not (member = head rest)) ; not in the rest
(good-set? rest))))) ; check the rest
If you need member, then
(define (member pred item list)
(and (not (null? list))
(or (pred item (car list))
(member pred item (cdr list)))))
I'm trying to figure out a question that takes a list of first names and a list of last name and create a new list of email addresses with the first letter of the first name and up to 7 letters of the last name and #yahoo.com.
example:
(check-expect
(emails-list (list "John" "Sarah") (list "King" "Dickinson"))
(list "jking#yahoo.com" "sdickins#yahoo.com"))
(check-expect (emails-list empty empty) empty)
So far I have:
(define (appendnames alof alos)
(cond [(and (empty? alof) (empty? alos)) empty]
[else (string-append
(substring (first alof) 0 1)
(cond [(< (string-length (first alos)) 8) (first alos)]
[else (substring (first alos) 0 7)])
"#yahoo.com")]))
(define (emails-list alof alos)
(cond [(and (empty? alof) (empty? alos)) empty]
[else (appendnames alof alos)]))
What I don't know how to do is how to make the first letters lowercase and where to put in the recursion so that appendnames will (appendnames (rest alof) (rest alos)).
Thanks so much for any help I can get!
Well, this looks like homework so I'll give you some hints, it'll be much better if you try to solve the problem by your own means. The first thing is - split the problem in two parts to make it simpler:
(define (emails-list alof alos)
(if <???> ; if any of the lists is empty
<???> ; return the empty list
(cons (make-email <???> <???>) ; create a new email with current values
(emails-list <???> <???>)))) ; and advance the recursion
The interesting part of course, will be creating the actual email. Refer to the character and string procedures available, here's the general idea:
(define (make-email name surname)
(<???> ; convert the whole string to lowercase
(<???> ; append the three parts of the email
(string (<???> name 0)) ; create a new string with the frist char in name
<???> ; create a substring with last name (*)
"#yahoo.com"))) ; add the email domain
The step marked with (*) requires a bit more explanation. Notice that we're interested in at most seven characters from the surname, which can be easily obtained with the substring procedure, as long as we remember that the end index is either 7 or the string's length, if the string's length is less than seven.
If you haven't already read it, you should read this chapter of HtDP, which explains how to design functions that take two complex inputs:
http://htdp.org/2003-09-26/Book/curriculum-Z-H-22.html
The HtDP chapter talks about three kinds of situations (or "cases") in which you need to process two complex arguments. Which situation matches the problem you're trying to solve?
How many cond clauses should you have? What are they? What do you have available to work with in the answer part of each clause? (In other words, what is the function's template?)
Once you get that far, it should be pretty easy to fill in the code. If you get stuck, use your concrete examples to help you.