小编tec*_*ref的帖子

使用REPL中的#-signs截断输出

我编写了一个按预期工作的函数,但我不明白为什么输出就是这样.

功能:

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)

sml smlnj

8
推荐指数
1
解决办法
1183
查看次数

prolog中的警告

我在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)

我能做些什么来避免它?

warnings prolog

8
推荐指数
1
解决办法
1875
查看次数

Prolog语言中的冒泡排序

我必须实现冒泡排序功能(排序算法)。

我已经实现了bubblesortswap,一个帮助功能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个小时。有谁知道我该怎么做?

prolog bubble-sort

3
推荐指数
1
解决办法
2万
查看次数

在SML中键入问题

我必须写一个"取消"列表的功能.

示例:输入[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)

sml smlnj

2
推荐指数
1
解决办法
498
查看次数

代码优化

我必须写一个函数"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("夜晚"))="(星期六和晚上)"

我的功能正常,但我有两个问题.

  1. 口译员告诉我 - > Warning: match nonexhaustive
  2. 我想我可以用所有类型的Locals函数编写函数(Not,And,Or)并避免重复代码,但我不知道如何.

有我的代码

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)

sml smlnj

1
推荐指数
1
解决办法
127
查看次数

C ++中的子类型多态性

我在 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)

c++ polymorphism subtype

0
推荐指数
1
解决办法
1013
查看次数

标签 统计

sml ×3

smlnj ×3

prolog ×2

bubble-sort ×1

c++ ×1

polymorphism ×1

subtype ×1

warnings ×1