I'm trying to create a function that will sum the digits of an integer in SML but I'm getting the following error.
Error: operator and operand don't agree [overload conflict]
operator domain: real * real
operand: [* ty] * [* ty]
in expression:
n / (d * 10)
I've tried to typecast the variables to real but it didn't work. Also I don't understand why I'm getting this error. Is not possible to use operators such as * and / with int and real in SML?
The code is the following:
fun sumDigits (n) =
if n < 10 then n
else
let
val d = 10
in
n mod d + sumDigits(trunc(n/(d*10)))
end
Looks like you have a few things wrong. To start, you'll want to use "div" rather than "/" when dividing integers. / is for reals. Also, trunc is a function for reals. 3rd, you'll want your recursive logic to just be sumDigits(n div 10), not sumDigits(n div (d*10)). You can also clean up the code by removing the d variable.
fun sumDigits (n) =
if n < 10 then n
else
n mod 10 + sumDigits(n div 10)
Related
While compiling simple cpp file I got an error. I want to write a function that changes celcius to farenheit.
double przelicznik(double n)
{
n = 1,8 * n + 32;
return n;
}
Also it doesn't give me a correct result.
The code is.
n = 1, (8 * n + 32)
The comma operator is a fairly uncommon mechanism where multiple expressions can be done in sequence.
correct code.
n = 1.8 * n + 32;
Having trouble writing a power function inStandard Ml. Im trying to write a function called exp of type int -> int -> int.
The application exp b e, for non-negative e, should return b^e.
For example, exp 3 2 should return 9. exp must be implemented with the function compound provided below. exp should not directly calls itself. Here is the compound function, it takes in a value n, a function, and a value x. All it does is it applies the function to the value x n number of times.
fun compound 0 f x = x
| compound n f x = compound (n-1) f (f x);
Im having trouble figuring out how to write this function without recursion, and with the restraint of having to use a function that only can use a function with one parameter. Anyone have any ideas of where to start with this?
This is what I have:
fun exp b 0 = 1
| exp b e = (compound e (fn x => x*x) b)
I know that this doesn't work, since if i put in 2^5 it will do:
2*2, 4*4, 16*16 etc.
You are extremely close. Your definition of exp compounds fn x => x*x which (as you noticed) is not what you want, because it is repeatedly squaring the input. Instead, you want to do repeated multiplication by the base. That is, fn x => b*x.
Next, you can actually remove the special case of e = 0 by relying upon the fact that compound "does the right thing" when asked to apply a function 0 times.
fun exp b e = compound e (fn x => b*x) 1
You could just do this instead I believe
fun exp 0 0 = 1
| exp b 0 = 1
| exp b e = (compound (e - 1) (fn x => b * x ) b);
this may not be exactly 100% proper code. I sort of just now read a bit of Standard ML documentation and took some code and reworked it for your example but the general idea is the same for most programming languages.
fun foo (num, power) =
let
val counter = ref power
val total = 1
in
while !counter > 0 do (
total := !total * num
counter := !counter - 1
)
end;
To be more clear with some pseudo-code:
input x, pow
total = 1
loop from 1 to pow
total = total * x
end loop
return total
This doesn't handle negative exponents but it should get you started.
It basically is a simple algorithm of what exponents truly are: repeated multiplication.
2^4 = 1*2*2*2*2 //The 1 is implicit
2^0 = 1
This is a homework. OCaml seems to be made by a psychopath.
let prime : int -> bool
= fun n ->
if n > 2 then
let a = n - 1 in
let rec divisor n a =
if a > 1 && n mod a = 0 then false
else if a = 2 && n mod a <> 0 then true
else divisor n a-1 ;;
else if n = 2 then true
else if n = 1 then false
I am not good at coding and I know that my isPrime algorithm is wrong.
But I wonder where in my code is the mistake that produces the syntax error.
Also is there any way to define the isPrime function in a recursive form?
Example:
let rec prime n = ~
You'll get better responses from experts if you don't gratuitously insult their language :-) But I'm an easygoing guy, so I'll take a stab at your syntax error.
There are quite a few problems in this code. Here are 3 that I see right off:
The symbol ;; is used to tell the interpreter that you've entered a full expression that you want it to evaluate. It's definitely out of place in the middle of a function declaration.
Your second let doesn't have an associated in. Every let must have an in after it. The only exception is for defining values at the top level of a module (like your prime function).
The expression divisor n a-1 is parsed as (divisor n a) - 1. You want parentheses like this: divisor a (n - 1).
I'm trying to write an if statement like this
if(denominator([(i-1)! + 1] / i)-1,print(hi),print(ho))
i can be any integer, for example 10. When I set i to 10 it gives this error:
? [(x-1)! + 1] / x
*** this should be an integer: [(x-1)!+1]/x
^-----------
I really only need to check if [(x-1)! + 1] / x is an integer or not. The denominator thing is what I came up with, I also tried Mod but couldn't get that working either.
It seems that you are confused with the names x and i.
Please, see that expression below works properly:
i = 10;
print([(i-1)! + 1] / i);
gp > [362881/10]
I'm not sure what the error was but I ended up using a floor function for determining if it was an integer or not.
You could use:
print(if(((i-1)! + 1) % i, "hi", "ho"))
If i (in your question x) is not an integer, you get an error from the ! (factorial) operator (but see gamma as well).
Do not use [] here, it creates a vector.
The opreator % which I used, gives the remainder. For example 11 % 4 gives the integer 3. In comparison Mod(11, 4) is not an ordinary integer, it is a member of the ring Z/4Z (integers modulo 4). That is very useful in many cases.
I supposed you wanted to write out strings, so I used quotes ". If hi and ho are variables, omit the quotes of course.
I'm trying to write an SML function that will return a list of all the prime factors to a given number. This will end up being a helper function to another function later on.
Originally the bigNumber is the number that I need to find the prime factors for and I pass in 1 less than that number as the divisor. Here's an example on how I'd call it to find the prime factors for the number 100. getPrimeFactors 100 99;
I'm not too worried about if there are flaws with the algorithm right now, but if you spot any errors with it I'd be happy to listen.
My main problem is trying to pass the return values up the recursion chain as lists and then combining those lists as they meet up with other lists.
fun getPrimeFactors bigNumber divisor =
if divisor > 0 then
if (bigNumber mod divisor) = 0 then List.concat(getPrimeFactors (bigNumber div divisor) ((bigNumber div divisor) - 1), getPrimeFactors divisor (divisor - 1))
else [getPrimeFactors bigNumber (divisor - 1)]
else [bigNumber];
Running this gives me this error.
C:.....\run.x86-win32.exe: Fatal error -- Uncaught exception Error with 0
raised at ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
C:\.....\commonFactors.sml:3.39-3.160 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list list
operand: 'Y * 'Y
in expression:
List.concat
((getPrimeFactors (<exp> div <exp>)) (<exp> div <exp> - 1),
(getPrimeFactors divisor) (divisor - 1))
[Finished in 0.4s with exit code 1]
Any help would be greatly appreciated!
You're trying to call List.concat on a tuple. The type of List.concat is
fn : 'a list list -> 'a list
That is, it takes a list of lists, concatenates all of those together, and returns the result. That's your error.
If, instead of using List.concat, we use the # operator, we get a different error (which may look slightly different on your system):
File "test.sml", line 7, characters 14-53:
! else [getPrimeFactors bigNumber (divisor - 1)]
! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
! Type clash: expression of type
! 'a list
! cannot have type
! 'a
! because of circularity
This error is because getPrimeFactors is supposed to return an int list, but here you're trying to stuff the result from getPrimeFactors into a list, thus getting int list list.
Just in case anyone was curious here's the corrected code with the right algorithm. Was able to fix the List.concat errors thanks to Tayacan.
fun getPrimeFactors big small =
if small > 1 then
if (big mod small) = 0 then List.concat[(getPrimeFactors (big div small) (big div small - 1)), (getPrimeFactors small (small - 1))]
else List.concat[(getPrimeFactors big (small - 1))]
else if big = 1 then nil
else [big];