Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename from_XXX to to_YYY. Fixed a bug of alpha-conversion.
- Loading branch information
nineties
committed
Mar 17, 2013
1 parent
10de8ad
commit 981302e
Showing
11 changed files
with
178 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
# Copyright (C) 2012 nineties | |||
# | |||
# $Id: random.ab 2013-03-17 16:51:44 nineties $ | |||
|
|||
module Std { | |||
# Choose one element randomly | |||
choose(seq): seq[rand(seq.size)] | |||
|
|||
# Choose `n' element from `seq' randomly | |||
choose(seq, n@Int) when n >= 0: { | |||
seq = to_array(seq) | |||
result: [] | |||
len: seq.size | |||
if (n > len) | |||
throw `Error{!LOCATION, "The sequence doesn't have " + n + " elements", seq} | |||
reverse_for(i in len-n..len-1) { | |||
j: rand(i+1) | |||
(seq[i], seq[j]) = (seq[j], seq[i]) | |||
result = cons(seq[i], result) | |||
} | |||
result | |||
} | |||
|
|||
# Choose `n' element from `seq' randomly | |||
choose(seq, n@Int, \repeat) when n >= 0: | |||
List::new(n, _ -> choose(seq)) | |||
|
|||
# Shuffle `seq' randomly by Fisher-Yates's algorithm. | |||
shuffle(seq): { | |||
seq = copy(seq) | |||
reverse_for (i in 1..(seq.size-1)) { | |||
j: rand(i+1) | |||
(seq[i], seq[j]) = (seq[j], seq[i]) | |||
} | |||
seq | |||
} | |||
|
|||
# Shuffle `seq'. | |||
shuffle(seq@List): { | |||
seq = to_array(seq) | |||
reverse_for (i in 1..(seq.size-1)) { | |||
j: rand(i+1) | |||
(seq[i], seq[j]) = (seq[j], seq[i]) | |||
} | |||
to_list(seq) | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.