Created
July 9, 2019 16:32
-
-
Save jaye-ross/a69c047da2d4109e307dd836887cca2b to your computer and use it in GitHub Desktop.
Create fibmorse sequence in racket
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
#lang racket | |
(define (next-value current-value) | |
(if (= current-value 0) | |
(if (= (random 2) 0) '(1) '(1 0)) | |
'(0 1))) | |
(define (do-sub myl) | |
(apply append | |
(map next-value myl))) | |
(define (gen-seq desired-len) | |
; local function iter | |
(define (iter init-lst desired-len) | |
(if (>= (length init-lst) desired-len) | |
(take init-lst desired-len) | |
(iter (do-sub init-lst) desired-len))) | |
; body of gen-seq calls iter | |
(iter (list (random 2)) desired-len)) | |
(define (filter-len myl by-val) | |
(length (filter (lambda (val) (= val by-val)) myl))) | |
(define (balance myl) | |
(- (filter-len myl 1) | |
(filter-len myl 0))) | |
; test | |
(define my-seq (gen-seq 20)) | |
(println my-seq) | |
(balance my-seq) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment