我编写了一个按预期工作的函数,但我不明白为什么输出就是这样.
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
(* XOR = (A And Not B) OR (Not A Or B) *)
local
fun do_xor (alpha,beta) = Or( And( alpha, Not(beta) ), Or(Not(alpha), beta))
in
fun xor (alpha,beta) = do_xor(alpha,beta);
end;
Run Code Online (Sandbox Code Playgroud)
val result = xor(Atom "a",Atom "b");
Run Code Online (Sandbox Code Playgroud)
val result = Or (And (Atom #,Not #),Or (Not #,Atom #)) : prop
Run Code Online (Sandbox Code Playgroud) 我在prolog中写了这个谓词:
list([]).
list([X|L]) :- list(L).
Run Code Online (Sandbox Code Playgroud)
它运作良好,但我得到了这个警告:
**Warning: /Users/hw6.pl:2:
Singleton variables: [X]** %
Run Code Online (Sandbox Code Playgroud)
我能做些什么来避免它?
我必须实现冒泡排序功能(排序算法)。
我已经实现了bubblesort和swap,一个帮助功能bubblesort:
swap([X,Y|T1],[Y,X|T1]):-(Y<X,!).
swap([X|T1],[X|T2]):- swap(T1,T2).
bubblesort([],[]) :- !.
bubblesort(T1,T2) :- (bubblesort(swap(T1,T2),T2)).
Run Code Online (Sandbox Code Playgroud)
我得到一个无限循环。我必须保留函数的签名:
冒泡排序(T1,T2)
我在这个问题上纠结了2个小时。有谁知道我该怎么做?
我必须写一个"取消"列表的功能.
示例:输入[7,[[8]],[[5,[9]]],6] - >输出(1,7),(3,8),(3,5),(4,9) ,(1,6)
我有这个功能,但我不能使用它,因为类型问题.
功能
datatype 'a superList = Elem of 'a
| List of 'a superList list;
local
fun un_nested( [] , n ) = []
| un_nested( (Elem x)::xs, n ) = (n, x) :: un_nested( xs, n )
| un_nested( (List x)::xs, n ) = un_nested( x, n + 1) @ un_nested(xs, n)
in
fun flat list = un_nested(list, 1)
end;
Run Code Online (Sandbox Code Playgroud)
这个例子
val test = List[List[Elem 2, List[Elem 3]]];
flat(test);
Run Code Online (Sandbox Code Playgroud)
错误
datatype 'a superList = …Run Code Online (Sandbox Code Playgroud) 我必须写一个函数"to_string",它接收这个数据类型
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
Run Code Online (Sandbox Code Playgroud)
并返回一个字符串.
例
秀和(Atom("星期六"),Atom("夜晚"))="(星期六和晚上)"
我的功能正常,但我有两个问题.
Warning: match nonexhaustive有我的代码
datatype prop = Atom of string | Not of prop | And of prop*prop | Or of prop*prop;
fun show(Atom(alpha)) = alpha
| show(Not(Atom(alpha))) = "(- "^alpha^" )"
| show(Or(Atom(alpha),Atom(beta))) = "( "^alpha^" | "^beta^" )"
| show(Not(Or(Atom(alpha),Atom(beta)))) = "(- ( "^alpha^" | "^beta^" ))"
| show(Or(Not(Atom(alpha)),Atom(beta))) = "( …Run Code Online (Sandbox Code Playgroud) 我在 C++ 中有这个程序。
ref2.h :
#ifndef REF2_H
#define REF2_H
#include <iostream>
using std::cout;
using std::endl;
int add_int_int(int a, int b) {return (a+b);}
class IntClass;
class Number {
public:
//return a Number object that's the results of x+this, when x is IntClass
virtual Number& addInt(IntClass& x) = 0;
//Print the number stored in the object
virtual void print_number() = 0;
}
class IntClass : public Number {
private:
int my_number;
public:
//Constructor
IntClass(int n):my_number(n) {}
//returns the number stored in the …Run Code Online (Sandbox Code Playgroud)