OSDN Git Service

truthy
authorsforman <sforman@hushmail.com>
Sat, 29 Jul 2023 17:19:39 +0000 (10:19 -0700)
committersforman <sforman@hushmail.com>
Sat, 29 Jul 2023 17:19:39 +0000 (10:19 -0700)
I have to check into this: the Python version has "bool" and no
"truthy".

implementations/Elm/src/Joy.elm

index 96625d2..41982f0 100644 (file)
@@ -52,6 +52,7 @@ joy_eval symbol stack expression =
         "stack" -> joy_stack stack expression
         "swaack" -> joy_swaack stack expression
         "swap" -> joy_swap stack expression
+        "truthy" -> joy_truthy stack expression
         _ -> Err ("Unknown word: " ++ symbol)
 
 
@@ -143,6 +144,28 @@ joy_swap stack expression =
         Err msg -> Err msg
 
 
+joy_truthy : JList -> JList -> Result String (JList, JList)
+joy_truthy stack expression =
+    case pop_any(stack) of
+        Ok (a, s0) ->
+            case a of
+                JoyTrue -> Ok (stack, expression)
+                JoyFalse -> Ok (stack, expression)
+                JoyInt i ->
+                    if 0 == i then
+                        Ok (JoyFalse :: s0, expression)
+                    else
+                        Ok (JoyTrue :: s0, expression)
+                JoyList el ->
+                    if [] == el then
+                        Ok (JoyFalse :: s0, expression)
+                    else
+                        Ok (JoyTrue :: s0, expression)
+                JoySymbol _ ->
+                    Err "Cannot Boolify."
+        Err msg -> Err msg
+
+
 push_int : Int -> JList -> JList
 push_int i stack = (JoyInt i) :: stack