compare1:[Int] -> Book
Compare1[x] =([x] == [x])
Test1 = scenario do
Debug(compare1 [11,12])
What's wrong with the above code why the error daml:44-1-30:Non-exhaustive patterns in function compare1 is appearing?
Let’s look at the crucial line here:
compare1 [x] = [x] == [x]
On the left side of the equal sign you have the pattern match [x]. This only matches a single element list and will bind that single element to the name x. So what the error is telling you that all other cases are not handled (empty lists and lists with more than one element).
To fix that you have two options, either you change the pattern match to just a variable xs (or any other name). That will match any list regardless of the number of elements and bind the list to the name xs.
compare1 xs = …
Alternatively, you can use 2 pattern matches to cover the case where the list is empty and the list has 1 or more elements:
compare1 [] = … -- do something for empty lists
compare1 (x :: xs) = … -- do something with the head of the list bound to `x` and the tail bound to `xs`
Related
I have the following haskell code:
Why doesn't x1's pattern matching to function f?
It's pretty hard to read as-is. Let's use some creative whitespace to line things up.
f ( [_ ]:[(x,[xs ])]:[y ,ys ] :[]) = 1
x1 = [[(1,[1,2])],[(1,[1,2])],[(1,[1,2]),(1,[1,2])],[]]
Okay. So there's actually a couple different things that aren't going as you expect!
[xs] does not match [1, 2], because [xs] is a one-element list and [1, 2] is a two-element list (possible fix: xs instead of [xs])
[y, ys] happens to match, but I suspect not in the way you intended: y matches to the first element of the list, just as I think you intend, but ys to the second element of the list, not the remainder of the list I think you intend (possible fix: (y:ys) instead of [y, ys])
your pattern's :[] matches the closing bracket of a list definition, not a final [] element (possible fix: :[]:[] instead of :[]; the first [] there matches the element, and the second [] matches the end-of-list marker)
So my first question was answered and it makes sense. It was able to output a list length after a sort, but originally I was asking as a way to use io:format function for a sort/0. But my next follow up, is how to use it with the sort/1? I've been able to work it out to give it, but it's giving it while recursion so I'm getting multiple lines and incorrectly. My question is how do I do the io:format once it's done with quick sort (also note, I also want the list to have no repeats) so I get just the one line of the length instead of the multiple lines I'm getting below?
Here's what I have and am getting:
-module(list).
-export([sort/1]).
sort([]) -> [];
sort([First|Rest]) ->
io:format("~nThe length of the list is ~w~n", [length([First]++Rest)])),
sort([ X || X <- Rest, X < First]) ++
[First] ++
sort([ X || X <- Rest, X > First]).
And the output:
56> list:sort([2,2,2,3,3,3,1,1,8,6]).
The length of the list is 10
The length of the list is 2
The length of the list is 5
The length of the list is 2
The length of the list is 1
[1,2,3,6,8]
So the sorted list with no duplicates is correct, but how do I fit the io:format function in there to show like this?
56> list:sort([2,2,2,3,3,3,1,1,8,6]).
[1,2,3,6,8]
The length of the list is 5
Unless I am mistaken, you are not going to be able to discriminate the usage of io:format/2 inside the recursive function as it is.
You could separate the printing from the recursive part.
sort(L)->
Result = quicksort(L),
io:format("~nThe length of the list is ~w~n", [length(Result)]),
Result.
quicksort([]) -> [];
quicksort([First|Rest]) ->
quicksort([ X || X <- Rest, X < First]) ++
[First] ++
quicksort([ X || X <- Rest, X > First]).
I would like to define a rule for proof by cases, to be used with proof (cases rule: <rule-name>). I managed to use the case_names and consumes parameters, but I did not manage to bind the schematic variable ?case, so that, inside a case of a proof using my rule, one can write show ?case .... How do I bind it?
Concretely: I have the Mizar-inspired notion of a trivial set, i.e. empty or singleton set. I would like to prove properties of trivial sets by empty vs. singleton case analysis. So far I have:
definition trivial where "trivial x = (x ⊆ {the_elem x})"
lemma trivial_cases [case_names empty singleton, consumes 1]:
assumes "trivial X"
assumes empty: "P {}"
and singleton: "⋀ x . X = {x} ⟹ P {x}"
shows "P X"
using assms unfolding trivial_def by (metis subset_singletonD)
and I can make use of this as follows:
notepad
begin
fix Q
fix X::"'a set"
have "trivial X" sorry
then have "Q X"
proof (cases rule: trivial_cases)
case empty
show "Q {}" sorry
next
case (singleton x)
show "Q {x}" sorry
qed
end
But I cannot use show ?case. If I try, it gives me the error message "Unbound schematic variable: ?case". print_cases inside the proof outputs the following:
cases:
empty:
let "?case" = "?P {}"
singleton:
fix x_
let "?case" = "?P {x_}"
assume "X = {x_}"
Which suggests that it doesn't work because ?P is not bound to trivial.
BTW: The full context in which I am using this can be seen at https://github.com/formare/auctions/blob/master/isabelle/Auction/SetUtils.thy.
As Joachim already mentioned, unlike induct, the cases method does not bind the schematic variable ?case. I would say the "canonical" way of conducting case analysis (as a proof method) conforms to this setup, since typically only different assumptions -- which taken together are exhaustive -- are considered, whereas the conclusion stays the same (abbreviated by ?thesis in Isabelle) throughout the different cases. I would set up your trivial_cases as follows:
lemma trivial_cases [consumes 1, case_names empty singleton]:
assumes "trivial X" and "X = {} ⟹ P" and "⋀x . X = {x} ⟹ P"
shows "P"
using assms by (auto simp: trivial_def)
Then you can use it like
notepad
begin
fix P and X :: "'a set"
have "trivial X" sorry
then have "P X"
proof (cases rule: trivial_cases)
case empty
then show ?thesis sorry
next
case (singleton x)
then show ?thesis sorry
qed
end
where the simplifier or explicit unfolding takes care of specializing X to {} and {x}, respectively.
Side Note: You can further tune trivial_cases by adding the attribtue cases pred: trivial. Then, whenever trivial ?X is the major assumption fed to cases, the rule trivial_cases is used implicitly, i.e., you can do the following
have "trivial X" sorry
then have "P X"
proof (cases)
in the above proof.
How can I define a cluster in Haskell using list comprehension?
I want to define a function for the cluster :
( a b c ) = [ a <- [1 .. 10],b<-[2 .. 10], c = (a, b)]
In your comment you gave the example [(1,2,1),(1,3,1),(1,4,1),(1,5,1),(1,6,1),(1,7,1)].
In that example, only the middle number changes, the other two are always 1. You can do this particular one with
ones = [(1,a,1)| a<-[1..7]]
However, you might want to vary the other ones. Let's have a look at how that works, but I'll use letters instead to make it clearer:
> [(1,a,b)| a<-[1..3],b<-['a'..'c']]
[(1,1,'a'),(1,1,'b'),(1,1,'c'),(1,2,'a'),(1,2,'b'),(1,2,'c'),(1,3,'a'),(1,3,'b'),(1,3,'c')]
You can see that the letters are varying more frequently than the numbers - the b<-[1..3] is like an outer loop, with c<-['a'..'c'] being the inner loop.
You could copy the c into the first of the three elements of the tuple:
> [(b,a,b)| a<-[1..3],b<-['a'..'b']]
[('a',1,'a'),('b',1,'b'),('a',2,'a'),('b',2,'b'),('a',3,'a'),('b',3,'b')]
Or give each its own varying input
> [(a,b,c)| a<-[1..2],b<-['a'..'b'],c<-[True,False]]
[(1,'a',True),(1,'a',False),(1,'b',True),(1,'b',False),(2,'a',True),(2,'a',False),(2,'b',True),(2,'b',False)]
F# function
Problem:
given a list of items e.g.:
["5";"10";"2";"53";"4"]
and a Search Index, I require a function such that it compares the current given index against its neighbor, returning the largest index
Example:
Given Index 1 will return Index value 2 (because 10 is greater than 5).
Given Index 4 will return Index 4 (because 53 is greater than 4)
Currently this is my function. It does not compile:
let GetMaxNode (x:Array) Idx = if x.[Idx] > x.[Idx+1] then Idx else If x.[Idx] < x.[Idx+1] then Idx+1
The errors I'm getting for all the x' are:
The field, constructor or member 'Item' is not defined (FS0039)
And also the second If:
The value or constructor 'If' is not defined (FS0039)
I suspect I'm still thinking in a procedural way, I was thinking about using pattern matching, however I was not confident enough with the syntax to try it.
Please can you also explain the answer as well, as I'm trying to learn F#, just the solution will not help me much.
Here's some code based on yours:
let GetMaxNode (x:_[]) idx =
if x.[idx] > x.[idx+1] then
idx
elif x.[idx] < x.[idx+1] then
idx+1
else
idx // same, return this one
The main changes are
to declare an array type, say <typename> []. In this case, we don't care about the type, so I use _ as a "don't care, please go infer the right thing for me" type variable.
"else if" is spelled elif in F#
need an else case for if equal
It is difficult to write solution to your problem in a functional style, because your problem is defined in terms of indices - when using functional data structures, such as lists, you don't usually refer to the elements by their index.
A functional version of your question would be, for example, to create a list that contains true when the element at the current position is larger than the next one and false when it is smaller. For your data this would give:
let data = [ 5; 10; 2; 53; 4 ]
let res = [ false; true; false; true; ] // no item to compare '4' with
This can be solved quite nicely using a recursive function that walks through the list and pattern matching (because pattern matching works much better with functional lists than with arrays)
let rec getMaxNodes data =
match data with
// list has at least two elements and current is larger
| current::next::other when current >= next ->
// process the rest of the list
let rest = (getMaxNodes (next::other))
// return 'true' followed by recursively processed rest of the list
true::rest
// list has at least two elements and current is smaller
| current::next::rest ->
// same as the previous case, but we return false
false::(getMaxNodes (next::rest))
| _ ->
// one element (so we cannot compare it with the next one)
// or empty list, so we return empty list
[]
getMaxNodes data
Here's the pattern matching version of Brian's answer.
let GetMaxNode (x:_[]) idx =
match idx with
| idx when x.[idx] > x.[idx+1] -> idx
| idx when x.[idx] < x.[idx+1] -> idx + 1
| idx -> idx // same, return this one
You may also see a syntax shortcut as you look at more F# code. The below code is functionally exactly the same as the above code.
let GetMaxNode (x:_[]) = function
| idx when x.[idx] > x.[idx+1] -> idx
| idx when x.[idx] < x.[idx+1] -> idx + 1
| idx -> idx // same, return this one
Whenever you start talking about indices, you are best sticking with Arrays or ResizeArrays; F# lists are not well-suited for operations on indices since they are singly-linked head to tail. That being said, it is not too difficult to write this algorithm in a purely functional way by moving through the list using a recursive loop and keeping track of the current index and current element.
let find elements index =
//a local tail-recursive function hides implementation details
//(cur::tail) is a pattern match on the list, i is the current index position
let rec loop (cur::tail) i =
if i = index then //when the current index matches the search index
if cur >= tail.Head then i //compare cur to tail.Head (which is next)
else (i+1)
else loop tail (i+1) //else continue
loop elements 0 //the entry point of loop and our return value
Use a list of ints instead of strings to get the results you expect (since "10" is actually less than "5"):
> let x = [5;10;2;53;4];;
> find x 0;;
val it : int = 1
> find x 1;;
val it : int = 1
> find x 2;;
val it : int = 3
> find x 3;;
val it : int = 3