F♭ (pronounced F-flat) is a toy language.
based on 99 Problems (solved) in OCaml
Find the last element of a list.
f♭> [ 'a' 'b' 'c' 'd' ] -1 @
[ 'd' ]
Find the last but one (last and penultimate) elements of a list.
f♭> [ 'a' 'b' 'c' 'd' ] -2 %
[ [ 'c' 'd' ] ]
Find the k’th element of a list.
f♭> [ 'a' 'b' 'c' 'd' ] 2 @
[ 'c' ]
Find the number of elements of a list.
f♭> [ 'a' 'b' 'c' 'd' ] ln
[ 4 ]
Reverse a list.
f♭> xxs: [ [ first ] [ rest ] bi ] ;
f♭> reverse: [ dup ln 1 <= [ ] [ xxs reverse swap + ] branch ] ;
f♭> [ 'a' 'b' 'c' 'd' ] reverse
[ [ 'd' 'c' 'b' 'a' ] ]
Find out whether a list is a palindrome.
f♭> [ 'r' 'a' 'c' 'e' 'c' 'a' 'r' ] dup reverse =
[ true ]
Flatten a nested list structure.
f♭> flatten*: [
dup array?
[ [ flatten* ] each ]
when
] ;
f♭> flatten: [ [ flatten* ] appl ] ;
f♭> [ 'a' [ 'b' [ 'c' 'd' ] 'e' ] ] flatten
['a' 'b' 'c' 'd' 'e']
Eliminate consecutive duplicates of list elements.
f♭> compress*: [
dup ln 0 >
[
xxs
[
dup2 =
[ drop ]
when
]
dip
compress*
]
[ drop ]
branch
] ;
f♭> compress: [ [ xxs compress* ] appl ] ;
f♭> ['a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd' 'e' 'e' 'e' 'e'] compress
['a' 'b' 'c' 'a' 'd' 'e']
Pack consecutive duplicates of list elements into sublists.
Run-length encoding of a list.
Modified run-length encoding.
Decode a run-length encoded list.
f♭> decode: [
dup array?
[ xxs swap * eval ]
when
] ;
f♭> decode: [ [ decode* ] map ] ;
f♭> [ [ 4 'a' ] 'b' [ 2 'c' ] [ 2 'a' ] 'd' [ 4 'e' ] ] decode
[ [ 'a' 'a' 'a' 'a' 'b' 'c' 'c' 'a' 'a' 'd' 'e' 'e' 'e' 'e' ] ]
Run-length encoding of a list (direct solution).
Duplicate the elements of a list.
f♭> [ 'a' 'b' 'c' 'c' 'd' ] [ dup ] map
[ [ 'd' 'c' 'b' 'a' ] ]
Replicate the elements of a list a given number of times.
f♭> [ 'a' 'b' 'c' 'c' 'd' ] 3 [ [ dup ] swap times ] >> map
[ [ 'a'
'a'
'a'
'a'
'b'
'b'
'b'
'b'
'c'
'c'
'c'
'c'
'c'
'c'
'c'
'c'
'd'
'd'
'd'
'd' ] ]
Drop every N’th element from a list.
Split a list into two parts; the length of the first part is given.
f♭> ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] 3 /
[ [ 'a' 'b' 'c' ] [ 'd' 'e' 'f' 'g' 'h' 'i' 'j' ] ]
Extract a slice from a list.
f♭> ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] 2 6 slice
[ [ 'c' 'd' 'e' 'f' ] ]
Rotate a list N places to the left.
f♭> ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j'] 3 / swap +
[ [ 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'a' 'b' 'c' ] ]
Remove the K’th element from a list.
f♭> ['a' 'b' 'c' 'd'] 1 / shift +
[ [ 'a' 'c' 'd' ] ]
Insert an element at a given position into a list.
f♭> ['a' 'b' 'c' 'd'] 1 'alfa' [ / ] dip swap >> +
[ [ 'a' 'alfa' 'b' 'c' 'd' ] ]
Create a list containing all integers within a given range.
f♭> range*: [ over over < [ countup ] [ countdown ] branch repn ] ;
f♭> range: [ [ range* ] >> appl ] ;
f♭> 4 9 range
[ [ 4 5 6 7 8 9 ] ]
Extract a given number of randomly selected elements from a list.
Lotto: Draw N different random numbers from the set 1..M.
Generate a random permutation of the elements of a list.
Generate the combinations of K distinct objects chosen from the N elements of a list.
Group the elements of a set into disjoint subsets.
Sorting a list of lists according to length of sublists.
Determine whether a given integer number is prime.
f♭> prime?*: [ 2 [ dup2 2 ^ > [ dup2 % 0 > ] dip swap * ] [ ++ ] while 2 ^ < ] ;
f♭> 7 prime*?
[ true ]
Determine the greatest common divisor of two positive integer numbers.
f♭> gcd: [ [ dup 0 > ] [ dup bury % ] while drop ] ;
f♭> 20536 7826 gcd
[ 2 ]
Determine whether two positive integer numbers are coprime.
f♭> 20536 7826 gcd 1 =
[ false ]
Calculate Euler’s totient function φ(m).
f♭> coprime?: [ gcd 1 = ] ;
f♭> totient: [ dup integers swap [ coprime? ] >> filter length ] ;
f♭> 10 totient
[ 4 ]
Determine the prime factors of a given positive integer.
f♭> factors: [
[
2
[ dup2 2 ^ > ]
[
dup2 divisor?
[ tuck / over ]
[ next-odd ]
branch
] while drop
] appl
] ;
f♭> 315 factors
[ [ 3 3 5 7 ] ]
Determine the prime factors of a given positive integer (2).
Calculate Euler’s totient function φ(m) (improved).
Compare the two methods of calculating Euler’s totient function.
f♭> [ 10090 phi ] timefn
'6.578947368421052 ops/sec'
f♭> [ 10090 phi-improved ] timefn
'8.771929824561404 ops/sec'
A list of prime numbers.
Goldbach’s conjecture.
A list of Goldbach compositions.
Truth tables for logical expressions (2 variables).
Truth tables for logical expressions.
Gray code.
Huffman code