syntax

Ulf Wiger <>
Fri Jan 28 09:47:22 CET 2000


On Fri, 28 Jan 2000, Kurt Wilkin wrote:

kurt>Hello all.
kurt>
kurt>I recently read this version of selection sort in a Haskell
kurt>algorithms book (from memory):
kurt>
kurt>selsort [] = []
kurt>selsort xs = m : (selsort (delete m xs))
kurt>		where m = min(xs)
kurt>						
kurt>and am wondering if anyone can give tips on how this 
kurt>is done, or where I can find how this is done, in Erlang.  

First of all, variables in Erlang always start with uppercase.


kurt>Attempting to translate directly I have started with: 
kurt>						 
kurt>selsort (xs) -> [m | selsort (lists:delete m xs)] 
kurt>                ..._now what?_...;
kurt>
kurt>or : 
kurt>
kurt>selsort (xs) -> 
kurt>		m = lists:min(xs) in 
kurt>                [m | selsort (lists:delete m xs)]

This is close, but replace the "in" with a comma.


Here is a working program:

------------------
-module(selsort).
-export([sort/1]).

sort([]) -> [];
sort(L) ->
    Min = lists:min(L),
    [Min | sort(lists:delete(Min, L))].
------------------

Erlang (BEAM) emulator version 4.9.1

Eshell V4.9.1  (abort with ^G)

1> c(selsort).                         
{ok,selsort}
2> selsort:sort([4,3,5,6,7,2,3,1,4,3]).
[1,2,3,3,3,4,4,5,6,7]

/Uffe
-- 
Ulf Wiger, Chief Designer AXD 301         <>
Ericsson Telecom AB                          tfn: +46  8 719 81 95
Varuvägen 9, Älvsjö                          mob: +46 70 519 81 95
S-126 25 Stockholm, Sweden                   fax: +46  8 719 43 44




More information about the erlang-questions mailing list