Question

Using Racket, write a bridgely? function that takes a list of at least 3 numbers and...

Using Racket, write a bridgely? function that takes a list of at least 3 numbers and returns #true if it is bridgely, otherwise #false. A list of numbers is called "bridgely" if it contains at least 3 numbers, and every number in the list,
except for the first and the last, is greater than both the first and the last number.

Thus, these lists are bridgely:
(list 1 2 3 4 5 6 5 4 3 2 1)
(list 0 4 4 4 4 4 2)

And these are not:
(list 1 2 3 4 5)
(list 1 1 2 3 4 3 2 1)

HERE IS MY CODE SO FAR BUT IT IS NOT CORRECT

; List of Numbers -> Numbers
; Produces the last number of the given list
(define (last-item lon)
(cond
[(empty? (rest lon)) (first lon)]
[else (last-item (rest lon))]))

; List of Numbers -> Boolean
; Produces true if list is a bridgley and false if list is not
(define (bridgely? lon)
(cond
[(empty? (first (first (rest lon)))) #f]
[(and (< (first lon) (first (rest lon)))
(< (last-item lon) (first (rest lon))))
(bridgely? (cons (first lon) (rest lon)))]
[else #f]))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

check out the code and do comment if any queries.

---------------------------------------------------------

; function definition
(define (bridgely? lst fst last)
; 3 conditions defined in function
; 1. length == 1 : only last element is remaining so return true
; 2. compares each element with first and last, if true then recursively call the function
; 3. if above 2 conditions fails then return false
(cond
[( = (length lst) 1) (write #t)]
[(and ( > (first lst) fst) ( > (first lst) last)) (bridgely? (rest lst) fst last)]
[else (write #f)]))


; define a list and print it
(define lst (list 1 2 3 4 5 6 5 4 3 2 1))
(writeln lst)
; extract first and last element of the list
(define fst (first lst))
(define last (first (reverse lst)))

; list should contain minimum 3 elements, if true then function call else return false
(if ( >= (length lst) 3) (bridgely? (rest lst) fst last) (write #f))

---------------------------------------------------------------------------------------------------

code and output :

function definition (define (bridgely? 1st fst last) 3 conditions defined in function 1: only last element is remaining so re

Add a comment
Know the answer?
Add Answer to:
Using Racket, write a bridgely? function that takes a list of at least 3 numbers and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Write in C++: Please create a shopping list using the STL list container, then follow the...

    Write in C++: Please create a shopping list using the STL list container, then follow the following instructions. Create an empty list. Append the items, "eggs," "milk," "sugar," "chocolate," and "flour" to the list. Print the list. Remove the first element from the list. Print the list. Insert the item, "coffee" at the beginning of the list. Print the list. Find the item, "sugar" and replace it with "honey." Print the list. Insert the item, "baking powder" before "milk" in...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • Write the function deep_contains. It takes as input a number and a list. That list might...

    Write the function deep_contains. It takes as input a number and a list. That list might contain lists as elements, and those lists might contain lists, etc. The deep_contains' function should return a Boolean indicating whether the number is contained inputted list, or a sublist of it, or a sublist of that, and so forth. For example: deep_contains (3, (1,3,5]) returns True deep_contains(3, 11, [3,5]]) returns True deep_contains (3, 1, [[3,4],5),6]) returns True deep_contains (2, (1,3,5]) returns False This function...

  • Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns...

    Write another function buddies(...) that receives a list (with at least 3 integer numbers), and returns the number of pairs of buddies in the list. We will define a “pair of buddies” to be the pair of elements that are contiguous in the list and that are equal, and also so that the contiguous elements to the pair are different (or the pair is in an extreme of the list). For example, [5,2,2,3] has 1 pair of buddies (the 2’s)...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Define a function that creates a list of all the numbers that are factors of the...

    Define a function that creates a list of all the numbers that are factors of the user's number. For example, if the function is called factor, factor(36) should return [1, 2, 3, 4, 6, 9, 12, 18, 36]. The numbers in your list should be sorted from least to greatest, and 1 and the original number should be included. Remember to consider negative numbers as well as 0. Bonus: Have the program print the factors of the users number in...

  • In racket language, define a procedure named build-naturals that returns the list (list 0 .. (-...

    In racket language, define a procedure named build-naturals that returns the list (list 0 .. (- n 1)) for any natural number n. Example: (build-naturals 5) returns (0 1 2 3 4).. The procedure must call build-list. The procedure passed to build-list must be a lambda expression, not a named procedure

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT