Sha256: d2431731dc031f715d6bb1b76f7cf1c0dc657a61f5caea12a3459818638895f0

Contents?: true

Size: 943 Bytes

Versions: 396

Compression:

Stored size: 943 Bytes

Contents

(defpackage #:trinary
  (:use #:common-lisp)
  (:export #:to-decimal))

(in-package #:trinary)

(defun to-decimal (string)
  "Return the decimal value for STRING which is in trinary. If STRING
is not a valid trinary number then return 0."
  (or (ignore-errors (parse-integer string :radix 3))
      0))

;;;
;;; Another possible implementation, if one wanted to reimplement
;;; PARSE-INTEGER
;;;
;; (defun to-decimal (string)
;;   (loop with revstr = (reverse string)
;;      for idx below (length string) and c across revstr
;;      for digit = (digit-char-p c 3)
;;      if (not digit) return 0
;;      else summing (* digit (expt 3 idx))))

;;;
;;; Another possible implementation but has a security flaw.
;;; Ponder what would happen if STRING was
;;; "#.(delete-file \"/etc/passwd\")"
;;;
;; (defun to-decimal (string)
;;   (let* ((*read-base* 3)
;;          (value (read-from-string string)))
;;     (if (typep value 'number) value 0)))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.179 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.178 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.177 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.176 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.175 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.174 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.173 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.172 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.171 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.170 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.169 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.167 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.166 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.165 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.164 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.163 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.162 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.161 tracks/common-lisp/exercises/trinary/example.lisp
trackler-2.2.1.160 tracks/common-lisp/exercises/trinary/example.lisp