Recursion

In your favorite programming language, write a recursive function that returns the minimum number from a list of integers without modifying the list.

Perl 6:

sub find-minimum(@a) {
return @a.first if @a.elems == 1;
@a.first < find-minimum(@a.tail: *-1)
?? @a.first
!! find-minimum(@a.tail: *-1);
}

# abstracting the comparison away
sub infix:($a, $b) { $a < $b ?? $a !! $b }
sub find-minimum(@a) {
return @a.first if @a.elems == 1;
return @a.first find-minimum(@a.tail: *-1);
}

Attached: chin-recursion.jpg (640x640, 40K)

perl 6 looking pretty ugly there :( not sure if my code is much better though

elixir
def min([]), do: raise("can't min empty list")
def min([x | xs]), do: _min(x, xs)
defp _min(m, []), do: m
defp _min(m, [x | xs]) when x < m, do: _min(x, xs)
defp _min(m, [_x | xs]), do: _min(m, xs)

No need to recurse:
min(a)


But if you insist:
def find_minimum(a):
if len(a) == 1:
return a[0]

return min(a[0], find_minimum(a[1:]))

Using a built in language/library function is cheating in this exercise. It defeats the point.

Write it in assembler then, nerd

Could have been a simple Math.min or a for loop but here

Attached: Code_2019-04-08_17-20-56.jpg (848x844, 75K)

there's no recursion in assembly
recursion is a higher level construct

shhhhhhhhhh

>there's no recursion in assembly
what is call

function getmin(x, first=1, last=length(x))
if first>last
nothing
elseif first==last
x[first]
else
min(x[first], getmin(x, first+1, last))
end
end


Superior due to not copying the array so at least it's not an O(n^2) algorithm. Not superior due to not being tail call and no tail call optimization in Julia anyway.

sorry, you're right, there's call
enjoy your stack overflow then, since there's no way to do tail optimization in assembly

>write it in assembly if you want to challenge yourself in your favorite language!
I'm not sure if you quite grasp the difference in your logic and his but I hope that making all the context of your post explicit helps you see it. If not then may whatever you believe in have mercy on your soul.

>stack overflow
>in a language where you can simply subtract the stack pointer manually and optimise by hand

>do something trivial in a language you already know
>challenge yourself
pick one

He's just upset that someone dare corrects him and is lashing out by posing a more difficult challenge.

damn, that's a lot like prolog
min(A,[X|L]):-
min(A,X,L).
min(A,A, []).
min(A, M, [X|L]):-
M < X,
min(A,M,L).
min(A,M,[X|L]):-
M > X,
min(A,X,L).

I think this works
prolog:

```
min(X,Y, Y):-X=Y.

min_list([X], X).
min_list([X|XS], Z):-min_list(XS, Y), min(X, Y, Z).
```

function sub(min, arrSize,array) {
if(arrSize==0) return min;
else {
if(array[arrSize]

POSIX compliance
foo() {
local a b this
a="$1"
b="$2"
if [ -z "$a" -o -z "$b" ]; then
return
fi
shift 2
if [ $a -eq $b -o $a -lt $b ]; then
this=$a
else
this=$b
fi
if [ $# -eq 0 ]; then
echo $this
fi
foo $this $*
}

foo 9 5 4 5 3 2 7 6 3 7 6 9 8 3 8 4 2 5

oops i meant arrSize==-1, min could at first index

prolog is a very beautiful language.

impressive
for some reason i can never figure out how to program in bash

This is the power of parens


#lang racket

(define (min lst1)
(define (iter-min lst2 act-min)
(define (new-min num) (< num act-min))
(cond [(null? lst2) act-min]
[(new-min (car lst2)) (iter-min (cdr lst2) (car lst2))]
[else (iter-min (cdr lst2) act-min)]))
(if (null? lst1) lst1
(iter-min lst1 (car lst1))))

minimum' :: ord a => [a] -> a
minimum' [] = error "Cannot get minimum of empty list"
minimum' [x] = x
minimum' (x:xs) = min x (mininum' xs)

Why limit myself to integers?

>using min

Fixpoint min : (n1 n2 : nat) : nat :=
match (leb n1 n2) with
| true => n1
| false => n2
end.

Fixpoint find_min : (l : list nat) (default : nat): nat :=
match l with
| [] => default
| h :: t => min(h (find_min t default)
end.

nevermind, i guess it's part of the ord typeclass?

It just compares two things and returns the lower but fine
min' :: Ord a => a -> a -> a
min' a b = if a > b then b else a

Love bash. It's like I'm an orchestrator for a symphony.

The biggest reason I left .NET. you've got a miniscule amount of code eating up so much screen space. I understand we have scroll wheels but holy fuck is that agonizing to look at.

Even when I used c# in highschool I still
function {
// }

min(list) = reduce(x,y-> if (x= 1) first(list) else f(first(list),reduce(f,tail(list)) end

I would generally use higher order functions from the standard library instead of doing raw recursion though, since the latter tends to encourage big monolithic functions that are annoying to debug instead of single-expressions.

oops, condition in reduce should be length(list)

Good points for using reduce. I dunno what lang that is but wouldn't if (length(list) >= 1) first(list) fail with an empty list? But yeah I get your point

Sure. Perl 6 has `min` routine too but I thought using it would defeat the purpose of the exercise.

I guess I am wondering how first(list) is defined in your lang for an empty list. In Racket and Scheme it's not allowed of course

that's javascript

It would throw an exception due to empty list, which is the correct behaviour for the min function (i.e. using the version of reduce which takes no starting value and requires a nonempty list).

Alright. What lang is this?

This might be better but your Haskell (?) implementation is still probably more general:

multi min(Num $a, Num $b) { $a < $b ?? $a !! $b }
multi min(Str $a, Str $b) { $a lt $b ?? $a !! $b }
multi find-minimum(@a where *.elems == 1) { @a.first }
multi find-minimum(@a) {
min(@a.first, find-minimum(@a.tail: *-1));
}

Point stands. Atrocious.

Well I'm the opposite, I can't stand that style you mention and I have to convert it to my style every time I copy some code. I just think it looks weird, the brackets being on the same x axis give better indication of code segments. But that's just like my opinion man.

Attached: 5C2D370A-8F6A-42E4-AC8C-63DF41C1A078.jpg (236x214, 6K)

is perl6 worth learning? i mean, there aren't any jobs in it..

>another perl6 user on Jow Forums
Finally there are two of us

function minimum(list){
function findMin(list, i, min){
if(i === list.length){
return min;
}else{
return findMin(list, i + 1, list[i] < min ? list[i] : min);
}
}
return findMin(list, 0, list[0]);
}

You know what gives a good indication of the code segments? Indenting it properly.

with iterators, won't allocate and will work on any type that can be somewhat compared
fn min(list: &[T]) -> Option where T: PartialOrd {
let mut it = list.iter();
it.next().map(|head| {
it.fold(head, |cur, n| if cur.gt(n) { n } else { cur })
})
}

or you know, just
list.iter().min()

Attached: pretty hard.jpg (720x428, 35K)

function minimum(list)
return #list == 1
and list[1]
or list[1] < minimum({table.unpack(list, 2, #list)})
and list[1]
or minimum({table.unpack(list, 2, #list)})
end

This isn't bash.

proc min {list} {
tailcall min+ $list 0 [lindex $list 0]
}
proc min+ {list lindex min} {
if {$lindex == [llength $list]} {
return $min
}
set min [expr {min($min,[lindex $list $lindex])}]
tailcall min+ $list [incr lindex] $min
}

>still cares about POSIX compliance

That's a major league oof from where I'm standing fella

#!/usr/bin/fish

function minimum
if test (count $argv) -eq 1
echo $argv[1]
return
end

set r (minimum $argv[2..-1])

if test $argv[1] -lt $r
echo $argv[1]
else
echo $r
end
end

minimum 9 5 4 5 3 2 7 6 3 7 6 9 8 3 8 4 2 5

Here's Mercury, to Prolog as Haskell is to Scheme:
:- pred min(int::out, list(int)::in) is nondet.
min(X, [X]).
min(if X

def find_min([head | tail], min \\ false) when head < min, do: find_min(tail, head)
def find_min([head | tail], min), do: find_min(tail, min)
def find_min([], min), do: min || raise("Empty list")


tfw haven't wrote elixir in like 2 months, feels weird having to re-think how flow works, my first one I just had a cond and then I refactored it.

from random import randint
op = 0
faggot = randint(op, 8999)
if op == faggot:
print op
else:
cuck = list(range(op, faggot))
print cuck[op]

min(x,y)=x>y?x:y
min(...a)=reduce(min, a)

I'm still learning recursion with python
def f(L, length, current_index, current_minimum):
if current_index == length-1:
return current_minimum
else:
for i in range(length):
if i < current_index:
pass
else:
if L[i] < current_minimum:
current_minimum = L[i]
return f(L, length, i+1, current_minimum)
else:
return f(L, length, i+1, current_minimum)
break


l = [1,8,6,4,7,-2,2,6,4,8]
print(f(l, len(l), 0, l[0]))

This outputs -2, so I think it is correct

I wish assembly was taught in intro to programming more often. It would dispel the struggles people have when trying to fathom such concepts. Setting up a stack frame context and being able to see the actual mechanics that make it work beats the top down approach, especially in a debugger. My first experience with recursion was in assembly, so it didn't seem like magic to me unlike to other high level programmers. All of these problems new comers have with recursion, pointers and understanding virtual memory layout should be learned under assembly.

This. Literally this

function min(iterable) {
const it = iterable[Symbol.iterator]();
return function step({ value, done }, min) {
return done ? min : step(it.next(), min > value ? value : min);
}(it.next(), Infinity);
}

English:
Given a list of numbers L, an index I, and a current smallest number N:
a) The number in the I-th position (I=1 -> 1st) is X.
1) If I is 0, return N.
3) If X

int min(int *tab, int n) {
if(n == 1) return tab[0];
else {
int l = min(tab, n/2);
int r = min(tab+n/2, n-n/2);
return l < r ? l : r;
}
}


I used to do C stuff like this a lot in my high school years. Can't say it's any useful in my actual programming job but it's pretty fun nonetheless.

def min(list: List[Int], minimum: Int = Int.MaxValue): Int = list match {
case head :: tail => min(tail, if(head < minimum) head else minimum)
case Nil => minimum
}

def l_min(xs):
if len(xs) == 1:
return xs[0]
elif xs[0] < x = l_min(xs[1:])::
return xs[0]
else:
return x

let f (x:xs) = if xs == [] then x else if x < min then x else min where min = f xs
I haven't programmed in haskell since I dropped out of college

Lua the best!!