Domanda

Cercando di capire come ottenere la radice quadrata in SML usando questo pseudocodice:

sqrt x s = s if s*s <= x
sqrt x s = sqrt x (s-1) if s*s > x

Questa formula ci è stata data dal docente, dobbiamo risolvere il problema in SML.

L'ho provato in diversi modi, ma posterò i diversi modi con gli errori tramite la console:

- fun sqrt x s = if ((s*s) <= x) then s | sqrt x s = if (s*s) > x then sqrt x (s - 1);
stdIn:32.39-32.45 Error: syntax error: deleting  BAR ID
stdIn:32.52-32.56 Error: syntax error: deleting  IF LPAREN
stdIn:32.59-32.62 Error: syntax error: deleting  RPAREN ID
stdIn:32.65-32.74 Error: syntax error: deleting  THEN ID
- fun sqrt x s = s if ((s*s) <= x) | sqrt x (s - 1);
stdIn:10.2-17.4 Error: syntax error: deleting  IF LPAREN
stdIn:17.14 Error: syntax error found at RPAREN
- fun sqrt x s = s if s*s <= x | sqrt x (s - 1);
stdIn:10.2-17.4 Error: syntax error: deleting  IF ID
- fun sqroot x s = s if s*s <= x | sqrt x (s - 1);
stdIn:17.2-17.6 Error: syntax error: deleting  IF ID
- fun sqrt x s = s IF s*s <= x | sqrt x (s - 1);
= ;
= ;
= ;;
stdIn:17.28-34.2 Error: syntax error: deleting  SEMICOLON SEMICOLON SEMICOLON
- fun sqrt x s = s IF s*s <= x END | sqrt x s = sqrt x (s-1) IF s*s > x END;
stdIn:17.12-17.15 Error: unbound variable or constructor: END
stdIn:10.2-17.2 Error: unbound variable or constructor: IF
stdIn:35.20-35.23 Error: unbound variable or constructor: END
stdIn:35.9-35.11 Error: unbound variable or constructor: IF
stdIn:1.17-17.15 Error: operator is not a function [circularity]
  operator: 'Z
  in expression:
    (s <errorvar>) s
stdIn:1.17-17.15 Error: operator and operand don't agree [overload]
  operator domain: 'Z * 'Z
  operand:         _ * (_ -> 'Y)
  in expression:
    (s <errorvar>) s * s
stdIn:1.6-35.23 Error: types of rules don't agree [literal]
  earlier rule(s): (_ -> int) * (_ -> 'Z) -> bool
  this rule: (_ -> int) * int -> bool
  in rule:
    (x,s) => (<exp> <exp>) s * s > x <errorvar>
stdIn:1.6-35.23 Error: right-hand-side of clause doesn't agree with function result type [literal]
  expression:  (_ -> 'Z) -> bool
  result type:  int -> _ -> int -> int
  in declaration:
    sqrt = (fn arg => (fn <pat> => <exp>))

Le linee contrassegnate con un '-' sono un input rapido da me e tutti gli altri vengono restituite dal sistema interattivo.

Qualsiasi aiuto è apprezzato grazie.

È stato utile?

Soluzione

@popovitsj ha una buona inclinazione.

SML utilizza la corrispondenza dei pattern per determinare quale versione della definizione della funzione utilizzare.

fun sqrt x s = if ((s*s) <= x) then s | sqrt x s = if (s*s) > x then sqrt x (s - 1);

Non è deterministico e SML non ha la struttura di tornare indietro e provare un'altra definizione se quello che prova fallisce (cioè come Prolog).

In questo scenario puoi semplicemente usare

fun sqrt x s = if s * s <= x then s else sqrt x (s-1);

Questa funzione è logicamente valida solo se si passa un valore che, se originariamente,, fallirà l'espressione IF.

Non l'ho testato, ma credo che dovrebbe aiutare.

Altri suggerimenti

Forse questo è utile? Lo pseudocodice implementato in Python:

def sqrt(x,s):
    if (s*s <= x):
        return s
    return sqrt(x,s-1)

Per SML, non posso verificarlo, perché non ho un compilatore SML, ma forse questo funzionerebbe?

fun sqrt (x, s) = if s*s <= x then s else sqrt(x,s-1)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top