Skip to content

Instantly share code, notes, and snippets.

@jaye-ross
Created July 9, 2019 16:32
Show Gist options
  • Save jaye-ross/a69c047da2d4109e307dd836887cca2b to your computer and use it in GitHub Desktop.
Save jaye-ross/a69c047da2d4109e307dd836887cca2b to your computer and use it in GitHub Desktop.
Create fibmorse sequence in racket
#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