Are the following two function definitions equivalent? head1 [] = error "No head for empty lists!" head1 (x:_) = x head2 xs = case xs of [] -> error "No head for empty lists!" (x:_) -> x never always sometimes only for empty lists
Added by Julie A.
Close
Step 1
Both functions aim to return the head (first element) of a list. If the list is empty, they both return an error message. Show more…
Show all steps
Your feedback will help us improve your experience
Vincenzo Zaccaro and 91 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Vincenzo Z.
Using Scheme, write a function called "replace-first" that takes two arguments: an item and a list. The function should return a version of the list with its first element replaced by the item. If the list is empty (you can use the "null?" predicate to determine this), return the empty list.
Akash M.
What is wrong with this argument? Let $S(x, y)$ be "$x$ is shorter than $y$ ." Given the premise $\exists s S(s, \text { Max })$ , it follows that $S(\text { Max, Max })$ . Then by existential generalization it follows that $\exists x S(x, x),$ so that someone is shorter than himself.
The Foundations: Logic and Proofs
Rules of Inference
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
Watch the video solution with this free unlock.
EMAIL
PASSWORD