Giter Site home page Giter Site logo

emehcs's Introduction

Ivan Chien/Yestercafe - He/Him

Outlook Badage Gmail Badge LeetCode Badge Codewars Badge

Hi there, I am Ivan Chien. I like Rust and C++ and have used them to write some projects, you can find them below or on the repositories page.

I use NeoVim, VSCode, and Emacs. You can find out my configs:

I'd like to learn more about functional programming languages like Haskell and independent-type programming languages like Coq or Agda.

Recent Goals

  • to finish Coffee OJ
  • porting some major C++ projects to Rust

Interested in

  • Rust & Go
  • functional programming
  • programming languages
  • theorem proving

My Handworks

emehcs's People

Contributors

yestercafe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

emehcs's Issues

Environment closure variable hiding bug

Codes, Newton's method calc sqrt:

(define (abs n)
  (if (< n 0)
      (-0 n)
      n))

(define tolerance (/ 1 100000))
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (print f)
      (debug-get-function-env f)
      (print next)
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

(define dx (/ 1 100000000))
(define (deriv g)
  (lambda (x)
    (/ (- (g (+ x dx)) (g x))
       dx)))

(define (newton-transform g)
  (lambda (x)
    (- x (/ (g x) ((deriv g) x2)))))

(define (newtons-method g guess)
  (fixed-point (newton-transform g) guess))

(define (sqrt x)
  (newtons-method (lambda (y) (- (* y y) x))
                  1))

(sqrt 2)

The result is:

>>> (define (abs n)
	  (if (< n 0)
	      (-0 n)
	      n))
=> (function (abs n) (if (< n 0) (-0 n) n))
>>> (define tolerance (/ 1 100000))
=> 1e-05
>>> (define (fixed-point f first-guess)
	  (define (close-enough? v1 v2)
	    (< (abs (- v1 v2)) tolerance))
	  (define (try guess)
	    (let ((next (f guess)))
	      (print f)
	      (debug-get-function-env f)
	      (print next)
	      (if (close-enough? guess next)
	          next
	          (try next))))
	  (try first-guess))
=> (function (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (print f) (debug-get-function-env f) (print next) (if (close-enough? guess next) next (try next)))) (try first-guess))
>>> (define dx (/ 1 100000000))
=> 1e-08
>>> (define (deriv g)
	  (lambda (x)
	    (/ (- (g (+ x dx)) (g x))
	       dx)))
=> (function (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx)))
>>> (define (newton-transform g)
	  (lambda (x)
	    (- x (/ (g x) ((deriv g) x)))))
=> (function (newton-transform g) (lambda (x) (- x (/ (g x) ((deriv g) x)))))
>>> (define (newtons-method g guess)
	  (fixed-point (newton-transform g) guess))
=> (function (newtons-method g guess) (fixed-point (newton-transform g) guess))
>>> (define (sqrt x)
	  (newtons-method (lambda (y) (- (* y y) x))
	                  1))
=> (function (sqrt x) (newtons-method (lambda (y) (- (* y y) x)) 1))
>>> (define kk (lambda (y) (- (* y y) 2)))
=> (function (lambda y) (- (* y y) 2))
>>> (sqrt 2)
(function (lambda x) (- x (/ (g x) ((function (lambda x) (/ (- (g (+ x dx)) (g x)) dx)) x))))
(g . (function (lambda y) (- (* y y) x)))
(guess . 1)
(g . (function (lambda y) (- (* y y) x)))
(x . 2)
(newton-transform . (function (newton-transform g) (lambda (x) (- x (/ (g x) ((function (lambda x) (/ (- (g (+ x dx)) (g x)) dx)) x))))))
(newtons-method . (function (newtons-method g guess) (fixed-point (newton-transform g) guess)))
(deriv . (function (deriv g) (lambda (x) (/ (- (g (+ x dx)) (g x)) dx))))
(dx . 1e-08)
(fixed-point . (function (fixed-point f first-guess) (define (close-enough? v1 v2) (< (abs (- v1 v2)) tolerance)) (define (try guess) (let ((next (f guess))) (print f) (debug-get-function-env f) (print next) (if (close-enough? guess next) next (try next)))) (try first-guess)))
(tolerance . 1e-05)
(sqrt . (function (sqrt x) (newtons-method (lambda (y) (- (* y y) x)) 1)))
(abs . (function (abs n) (if (< n 0) (-0 n) n)))
1
=> 1

But in f's closure env, the x should be 1, not 2. Fix it.

Y-combinator codes can't eval successfully

(define Y
  (lambda (fn)
    ((lambda (f)
       (f f)) (lambda (f)
                (fn (lambda (s) ((f f) s)))))))

((Y (lambda (g)
      (lambda (s)
        (cond
          ((eq? s '()) '())
          (#t
            (cons (string-upcase (car s)) (g (cdr s)))))))) '("a" "b" "c" "d" "e"))

eval and env codes may/would be rewritten.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.