FInding minimum of a binary search tree in Racket - binary-search-tree

So, i am trying to find the least value of a binary search tree in racket and i keep getting this error:
cadr: contract violation
expected: (cons/c any/c pair?)
given: 'null
My code is the following:
(define minimum
(λ (tree)
(if (null? tree) null
(if (null? (cadr tree)) (car tree)
(minimum (cadr tree)))))
Each node has the structure (value, left, right).

It looks like a problem with your test data, not the minimum procedure itself. For instance, this works for me:
(define tree
(list 5 (list 3 (list 1 null null)
(list 4 null null))
(list 6 null null)))
(minimum tree)
=> 1

Related

sum of setof in prolog

I have this predicate to get the sum of the length of all borders of a country. I could solve it with findall but I have to use setof. My facts look like this:
borders(sweden,finland,586).
borders(norway,sweden,1619).
My code
circumference(C, Country) :-
findall(X, ( borders(Country, _, X) ; borders(_, Country, X)), Kms),
sum_list(Kms, C).
You cannot find the sum using bagof directly, all you can do is make a list and then sum that list (but you knew that already). In SWI-Prolog there is library(aggregate) that does the bagging and the summing for you. With the facts you have, you would write:
?- aggregate(sum(X), Y^( borders(Y, sweden, X) ; borders(sweden, Y, X) ), Circumference).
Circumference = 2205.
If you instead must obey the whims of your instructor and type "bagof" yourself, or if you are not allowed to use a modern, open source, comprehensive Prolog implementation, you can use the same approach with bagof and manually build the list before summing it:
?- bagof(X, Y^( borders(Y, sweden, X) ; borders(sweden, Y, X) ), Borders).
Borders = [1619, 586].
For reasons that are lost in the mists of time the funny thing with the Var^Goal that you see in both aggregate and bagof is called "existentially qualifying the variables in Goal". You might also read that "^ prevents binding Var in Goal". I cannot explain what this really means.
I ended up using this:
circumference(Z, Country) :- setof(X, Q^(borders(Q,Country,X);borders(Country,Q,X)),Border),
sum_list(Border,Z).
% Adds the numbers in a list.
sum_list([], 0).
sum_list([H|T], Sum) :-
sum_list(T, Rest),
Sum is H + Rest.

How to print values in Autolisp?

I'm writing my simple script in AutoCAD to select a table and to retrieve some data of the table with printing it in console.
(defun C:test (/)
(vl-load-com)
(setvar "CTAB" "Model")
(princ "\n*** Pick a table ***")
(if (setq ss (ssget "_:S:E:L" '((0 . "ACAD_TABLE"))))
(setq table (vlax-ename->vla-object (ssname ss 0)))
)
(setq table_rows (vla-get-rows table)
table_width (vla-get-width table)
)
(prinс (strcat "Num of rows is" table_rows ", table width is " table_width))
)
After running this script and picking a table of 57 rows in Autocad it returns me a mistake:
wrong type of argument: stringp 57
How could I fix it?
strcat needs string arguments. In Your case table_rows is integer and table_width looks like real. So You need to convert by itoa and
rtos
Try this:
(prinс (strcat "Num of rows is" (itoa table_rows) ", table width is " (rtos table_width)))

Error: Cannot find any non-recursive equality over Q0 in Coq

Theorem preservation : forall Pi e T1 S e' Q S', hasType empty Pi e T1 -> storeWellTyped Pi S -> step (conf (e :: Q) S) (conf (e' :: Q) S') -> exists Pi', (extends Pi' Pi /\ hasType empty Pi' e' T1 /\ storeWellTyped Pi' S'). – Amit 3 mins ago edit
When I am trying to prove this theorem e get replaced by various expressions in my syntax but e' does not get replaced by corresponding stepped expression. For example if e is e1;e2 then e' should be e1';e2 in the hypothesis. But e' does not changes so it becomes difficult to prove.
I was trying to do induction on the hypothesis hasType empty Pi e T1 and got the various cases for each expression to prove but due to above problem I could not proceed further.
Error: Cannot find any non-recursive equality over Q0.
I was trying to prove preservation theorem where I got this error. I don't really understand what it means and how it can be eliminated. This error pops out after using subst with inversion.

Clojure.contrib.sql: How do I retrieve the results of a select statement into a vector that can be accessed anytime?

For example, in this query:
(Clojure.contrib.sql/with-query-results rows ["SELECT count(*) from tableName"] (doseq [record rows] (println (vals record))))
It seems that record and rows don't exist outside this scope but I want it to exist anytime for me access.
Update:
I tried the following lines of code
(def asdf [])
(sql/with-connection db
(sql/with-query-results rows ["SELECT * FROM tableName"] (doseq [record rows] (def asdf (conj asdf record)))))
(println asdf)
Why does the print statement of asdf above return empty when I added the rows to it in the sql statement?
You seem to lack a basic understanding of Clojure's underlying principles, in particular immutable data structures. Your conj call doesn't modify the var asdf -- a var is not a variable. You could do something like the following (untested)
(def asdf
(sql/with-connection db
(doall (sql/with-query-results rows ["SELECT * FROM tableName"]))))
to store the result directly as the value of asdf, but this is probably not really what you want. Get familiar with Clojure's take on functional programming instead.

Partial SQL insert in haskelldb

I just started a new project and wanted to use HaskellDB in the beginning. I created a database with 2 columns:
create table sensor (
service text,
name text
);
..found out how to do the basic HaskellDB machinery (ohhh..the documentation) and wanted to do an insert. However, I wanted to do a partial insert (there are supposed to be more columns), something like:
insert into sensor (service) values ('myservice');
Translated into HaskellDB:
transaction db $ insert db SE.sensor (SE.service <<- (Just $ senService sensor))
But...that simply doesn't work. What also does not work is if I specify the column names in different order, which is not exactly conenient as well. Is there a way to do a partial insert in haskelldb?
The error codes I get are - when I just inserted a different column (the 'name') as the first one:
Couldn't match expected type `SEI.Service'
against inferred type `SEI.Name'
Expected type: SEI.Intsensor
Inferred type: Database.HaskellDB.HDBRec.RecCons
SEI.Name (Expr String) er
When using functional dependencies to combine
Database.HaskellDB.Query.InsertRec
(Database.HaskellDB.HDBRec.RecCons f (e a) r)
(Database.HaskellDB.HDBRec.RecCons f (Expr a) er),
etc..
And when I do the 'service' as the first - and only - field, I get:
Couldn't match expected type `Database.HaskellDB.HDBRec.RecCons
SEI.Name
(Expr String)
(Database.HaskellDB.HDBRec.RecCons
SEI.Time
(Expr Int)
(Database.HaskellDB.HDBRec.RecCons
SEI.Intval (Expr Int) Database.HaskellDB.HDBRec.RecNil))'
against inferred type `Database.HaskellDB.HDBRec.RecNil'
(I have a couple of other columns in the table)
This looks really like 'by design', unfortunately :(
You're right, that does look intentional. The HaskellDB.Query docs show that insert has a type of:
insert :: (ToPrimExprs r, ShowRecRow r, InsertRec r er) => Database -> Table er -> Record r -> IO ()
In particular, the relation InsertRec r er must hold. That's defined elsewhere by the recursive type program:
InsertRec RecNil RecNil
(InsertExpr e, InsertRec r er) => InsertRec (RecCons f (e a) r) (RecCons f (Expr a) er)
The first line is the base case. The second line is an inductive case. It really does want to walk every element of er, the table. There's no short-circuit, and no support for re-ordering. But in my own tests, I have seen this work, using _default:
insQ db = insert db test_tbl1 (c1 <<- (Just 5) # c2 << _default)
So if you want a partial insert, you can always say:
insC1 db x = insert db test_tbl1 (c1 <<- (Just x) # c2 << _default)
insC2 db x = insert db test_tbl2 (c1 << _default # c2 <<- (Just x))
I realize this isn't everything you're looking for. It looks like InsertRec can be re-written in the style of HList, to permit more generalization. That would be an excellent contribution.