New to SML and functional programming. Beginner questions - functional-programming

I'm trying to define the logical operation exclusive or as a two-variable function. I can't use constants, type annotations or if-then-else. I believe I'm supposed to do it by using logic operators, but am lost on how I'm supposed to write out a function just using those! So far I have
fun xor(a,b) = a orelse b andalso not(a andalso b).
How am I supposed to return a true value from this point?

First, so you don't need to remember the relative precedence and associativity of andalso and orelse I'd use more parens:
fun xor(a,b) = (a orelse b) andalso not(a andalso b);
- map xor [(true, true), (true, false), (false, true), (false, false)];
val it = [false,true,true,false] : bool list
Your error was that your expression associated as follows
a orelse (b andalso not(a andalso b));
There is no notion of 'returning' in sml. There are only expressions, which are evaluated to values, which are in turn used in other expressions.
(a orelse b) andalso not(a andalso b)
evaluates to a value of type bool. You can use that result in further computations:
if xor(foo, bor) then 1 else 2

Related

Comparitive operator not working in Julia script file when used in control flow with conditional operator

I have tried different operators <,>,etc. Nothing seems to work when I combine comparative operators with conditional operators.
e = readline()
if e == 0
println("e is equal to 0")
else
println("e is not equal to 0")
end
The expected result is obvious, if e = 0, prints e is equal to 0, if e != 0, prints e is not equal to 0.
However, it always prints the bottom line, e is not equal to 0.
That's because readline returns a string, which is never equal to an integer (by the definition of == Julia uses).
Here are some possible ways to achieve what you want:
Compare to a string literal instead: if e == "0"
Use tryparse: if tryparse(Int, e) == 0 (will return nothing if e is not a number literal)
Use parse, but with try/catch instead of an if:
try
p = parse(Int, e)
p == 0 ? println("e is 0") : println("e is not 0")
catch
println("not even an integer.")
end
The reason that the if returns the branch that you don't expect is those given to you by #phg (you got a String by readline()).
For my code I use the following function to parse user-provided data given in a terminal:
function getUserInput(T=String,msg="")
print("$msg ")
if T == String
return readline()
else
try
return parse(T,readline())
catch
println("Sorry, I could not interpret your answer. Please try again")
getUserInput(T,msg)
end
end
end
sentence = getUserInput(String,"Which sentence do you want to be repeated?");
n = getUserInput(Int64,"How many times do you want it to be repeated?");
[println(sentence) for i in 1:n]
println("Done!")

Combinatorics through Recursion: How to be functional

I am working on a combinatorics function. I want to input a string and output all possible combinations of that string using each character. For instance, this will print all the combinations of ME = {MM, ME, EM, EE}.
function combo_recursive(a)
arr_combo = split(a, "")
arr_size = size(arr_combo)
arr_max = arr_size[1]
mutab!e = []
function combo_recurse(b)
if b ≤ 1
for i in 1:arr_max
append!(mutab!e,arr_combo[i])
println(join(mutab!e))
pop!(mutab!e)
end
else
for j in 1:arr_max
append!(mutab!e,arr_combo[j])
combo_recurse(b-1)
pop!(mutab!e)
end
end
end
combo_recurse(arr_max)
end
It works fine but, I implemented mutable arrays to achieve the desired result. Any recommendations on how to apply a functional ethos to this??

Are constructors in the plain calculus of constructions disjoint and injective?

Based on this answer, it looks like the calculus of inductive constructions, as used in Coq, has disjoint, injective constructors for inductive types.
In the plain calculus of constructions (i.e., without primitive inductive types), which uses impredicative encodings for types (e.g., ∏(Nat: *).∏(Succ: Nat → Nat).∏(Zero: Nat).Nat), is this still true? Can I always find out which "constructor" was used? Also, is injectivity (as in ∀a b.I a = I b → a = b) provable in Coq with Prop or impredicative Set?
This seems to cause trouble in Idris.
(I am not sure about all the points that you asked, so I am making this answer a community wiki, so that others can add to it.)
Just for completeness, let's use an impredicative encoding of the Booleans as an example. I also included the encodings of some basic connectives.
Definition bool : Prop := forall (A : Prop), A -> A -> A.
Definition false : bool := fun A _ Hf => Hf.
Definition true : bool := fun A Ht _ => Ht.
Definition eq (n m : bool) : Prop :=
forall (P : bool -> Prop), P n -> P m.
Definition False : Prop := forall (A : Prop), A.
We cannot prove that true and false are disjoint in CoC; that is, the following statement is not provable:
eq false true -> False.
This is because, if this statement were provable in CoC, we would be able to prove true <> false in Coq, and this would contradict proof irrelevance, which is a valid axiom to add. Here is a proof:
Section injectivity_is_not_provable.
Variable Hneq : eq false true -> False. (* suppose it's provable in CoC *)
Lemma injectivity : false <> true.
Proof.
intros Heq.
rewrite Heq in Hneq.
now apply (Hneq (fun P x => x)).
Qed.
Require Import Coq.Logic.ProofIrrelevance.
Fact contradiction : Logic.False.
Proof.
pose proof (proof_irrelevance bool false true) as H.
apply (injectivity H).
Qed.
End injectivity_is_not_provable.

Tail recursion in SML does not present any output

Following my previous post here , I tried to do what was suggested and convert the code
into a Tail-recursion method with let .
The original code - which does not work (due to using val inside if condition) :
fun func() =
val decimal = 0 (* the final result *)
val multiple = 0 (* keeps track of multiples, eg. In XXV, X would be a multiple *)
val current = 0 (* the digit currently being processed *)
val top = 0 (* value of the last element in the list *)
val last_add = 0 (* the last digit that wasn't a multiple, or subtraction operation *)
val last_sub = 0
val problem = 0 (* if value is 1 then there is a problem with the input *)
val myList = [1,2,3,4,5] (* the list has more values *)
while (myList <> []) (* run while the list is not empty *)
val current = tl(myList) (* grab the last element from the list *)
val myList = tl(myList) (* remove the last element from the list *)
val top = tl(myList) (* grab the value at the end of the list *)
if ( myList <> []) andalso (current > top))
then
val decimal = decimal + current - top
val last_sub = top;
val myList = tl(myList)
else
if ( (myList = []) andalso (current = top))
then val decimal = decimal + current
val multiple = multiple + 1
else
if (last_sub = current)
then val problem = 1
else
val decimal = decimal + current
val multiple = 0
val last_add = current
And the code as a tail-recursion method :
fun calc [] = 0
|calc [x] = x
|calc (head::tail) =
let
val decimal = 0
val multiple = 0
val current = 0
val top = 0
val last_add = 0
val last_sub = 0
val problem = 0
val doNothing = 0
in
let
val current = hd(rev(head::tail)) (* grab the last element *)
val head::tail = rev(tl(rev(head::tail))) (* POP action - remove the last element from the list *)
val top = hd(rev(head::tail)) (* grab the new last element after removing *)
in
if (current > top) then
let
val decimal = decimal + current - top
val last_sub = top
val head::tail = rev(tl(rev(head::tail))) (* POP action - remove the last element from the list *)
in
calc(head::tail)
end
else
if ( (head::tail = []) andalso (current = top))
then let
val decimal = decimal + current
val multiple = multiple + 1
in
calc(head::tail)
end
else
if (last_sub <> current)
then let
val decimal = decimal + current
val multiple = 0
val last_add = current
in
calc(head::tail)
end
else
(* do nothing *)
val doNothing = 0
end
end;
However , when I try to enter :
calc([0,100,20,30,4,50]);
I get :
uncaught exception Bind [nonexhaustive binding failure]
raised at: stdIn:216.13-216.50
I know the code is very hard to read and pretty long , but it would be greatly appreciated
if someone could explain to me how to fix it , or help me find the reason for this output .
Thanks
You have a few issues with your code.
First of all, you can use last to grab the last element of a list. See the List documentation for more info. But unless you have a really good reason to do so, it's easier and much more efficient to simply start from the beginning of the list and pop elements off the beginning as you recurse. You already have the first element bound to head in your code using pattern matching.
Secondly, unless you use refs (which you probably don't want to do) there are no variables in Standard ML, only values. What this means is that if you want to carry state between invocations, any accumulators need to be parameters of your function. Using a helper function to initialize accumulators is a common pattern.
Third, instead of comparing a list to [] to test if it's empty, use the null function. Trust me on this. You'll get warnings using = because of subtle type inference issues. Better yet, use a pattern match on your function's parameters or use a case statement. Pattern matching allows the compiler to tell you whether you've handled all possible cases.
Fourth, SML typically uses camelCase, not snake_case, for variable names. This is more stylistic, but as you write more code and collaborate, you're going to want to fit with the conventions.
Fifth, when you do recursion on a list, don't try to look at multiple values in the list. This complicates things. Treat it as a head element and tail list, and everything will become much simpler. In my code, instead of keeping current in the list, I did this by splitting it out into a separate parameter. Have a base case where you simply return the answer from one of your accumulators, and a recursive case where you recurse with updated accumulator values and a single value popped from your list. This eliminates the problem scenario.
I'm not sure if this logic is correct since I don't know what you're trying to calculate, but check out this code which illustrates some of the things I talked about.
(* This is the helper function which takes accumulators as
parameters. You shouldn't call this directly. *)
fun calc' decimal _ _ _ _ [] =
(* We processed everything in the list. Just return the accumulator. *)
decimal
| calc' decimal multiple lastAdd lastSub current (top::tail) =
(* This case is for when there are 1 or more elements in the list. *)
if current > top then
calc' (decimal + current - top) multiple lastAdd top top tail
else if current = top then
calc' (decimal + current) (multiple + 1) lastAdd lastSub top tail
else
calc' (decimal + current) 0 current lastSub top tail
(* This is the function you should call. *)
fun calc [] = 0
| calc [_] = 0 (* Given a single-element list. *)
| calc (x::xs) =
(* Apply the helper with correct initial values. *)
calc' 0 0 0 0 x xs
In a functional language, instead of assigning to a variable when you want to change it, simply recurse and specify the new value for the correct parameter. This is how you write a "loop" in a functional language using recursion. As long as you only use tail-recursion, it will be just as efficient as a while loop in your favorite imperative language.

Sorting an Array of times in Classic ASP

had to return to a old app I had build in Classic ASP and do a few changes, however one bit is stumping me.
I have a string such as:
theTimings = "12:00-14:30,18:00-22:30,07:30-10:30"
I am turning this into an Array using
timingsArray=Split(theTimings,",")
However I need to sort this Array so the earliest times appear first, i.e.
07:30-10:30,12:00-14:30,18:00-22:30
Anyone got any idea how to do this?
You want to sort an array, Classic VBScript ASP does not offer that. So you got 4 options: Use JScript, gather from .NET, convert the array to a dictionary and sort there or "roll your own algorithm" (a.k.a. find something on the net).
Sorry, I don't know which one would be optimal or faster, but if you go with VBScript, I advise using quick sort. Here's an implementation of it that's applicable with strings that I've adapted from somewhere:
Dim prt
prt = Array("this", "array", "organized", "is", "not")
print_array(prt)
arr_sort prt
print_array(prt)
Sub arr_sort (arr)
Call QuickSort(arr, 0, ubound(arr, 1))
End Sub
Sub SwapRows (ary,row1,row2)
Dim tempvar
tempvar = ary(row1)
ary(row1) = ary(row2)
ary(row2) = tempvar
End Sub 'SwapRows
Sub QuickSort (vec,loBound,hiBound)
'==--------------------------------------------------------==
'== Sort a 1 dimensional array ==
'== ==
'== This procedure is adapted from the algorithm given in: ==
'== ~ Data Abstractions & Structures using C++ by ~ ==
'== ~ Mark Headington and David Riley, pg. 586 ~ ==
'== Quicksort is the fastest array sorting routine For ==
'== unordered arrays. Its big O is n log n ==
'== ==
'== Parameters: ==
'== vec - array to be sorted ==
'== loBound and hiBound are simply the upper and lower ==
'== bounds of the array's 1st dimension. It's probably ==
'== easiest to use the LBound and UBound functions to ==
'== Set these. ==
'==--------------------------------------------------------==
Dim pivot,loSwap,hiSwap,temp,counter
'== Two items to sort
if hiBound - loBound = 1 then
if vec(loBound) > vec(hiBound) then
Call SwapRows(vec,hiBound,loBound)
End If
End If
'== Three or more items to sort
pivot = vec(int((loBound + hiBound) / 2))
vec(int((loBound + hiBound) / 2)) = vec(loBound)
vec(loBound) = pivot
loSwap = loBound + 1
hiSwap = hiBound
Do
'== Find the right loSwap
while loSwap < hiSwap and vec(loSwap) <= pivot
loSwap = loSwap + 1
wend
'== Find the right hiSwap
while vec(hiSwap) > pivot
hiSwap = hiSwap - 1
wend
'== Swap values if loSwap is less then hiSwap
if loSwap < hiSwap then Call SwapRows(vec,loSwap,hiSwap)
Loop While loSwap < hiSwap
vec(loBound) = vec(hiSwap)
vec(hiSwap) = pivot
'== Recursively call function .. the beauty of Quicksort
'== 2 or more items in first section
if loBound < (hiSwap - 1) then Call QuickSort(vec,loBound,hiSwap-1)
'== 2 or more items in second section
if hiSwap + 1 < hibound then Call QuickSort(vec,hiSwap+1,hiBound)
End Sub 'QuickSort
And as a bonus, this is my "print_array", which I want to evolve into a fully working "print_r" someday:
public sub print_array (var)
call print_r_depth(var, 0)
end sub
public sub print_r_depth (var, depth)
if depth=0 then
response.write("<pre>" & Tab(depth))
response.write(typename(var))
end if
if isarray(var) then
response.write(Tab(depth) & " (<br />")
dim x
for x=0 to uBound(var)
response.write(Tab(depth+1) & "("&x&")")
call print_r_depth(var(x), depth+2)
response.write("<br />")
next
response.write(Tab(depth) & ")")
end if
select case vartype(var)
case VBEmpty: 'Uninitialized
case VBNull: 'Contains no valid data
case VBDataObject: 'Data access object
case VBError:
case VBArray:
case VBObject:
case VBVariant:
case else:
if vartype(var) < 16 then
response.write(" => " & var)
else
response.write(" - vartype:" & vartype(var) & " depth:" & depth)
end if
end select
if depth=0 then response.write("</pre>") end if
end sub
public function Tab (spaces)
dim val, x
val = ""
for x=1 to spaces
val=val & " "
next
Tab = val
end function

Resources