Are Lists Inductive or Coinductive in Haskell?

Due to laziness, Haskell types are both inductive and coinductive, or, there is no formal distinguishment between data and codata. All recursive types can contain an infinite nesting of constructors. In languages such as Idris, Coq, Agda, etc. a definition like ones = 1 : ones is rejected by the termination checker. Laziness means that … Read more

Program with loop will not terminate with CTRL + C

From the documentation of system() (emphasis/bold mine): The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl(“/bin/sh”, “sh”, “-c”, command, (char *) 0); system() returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and … Read more

Why does this Haskell code work successfully with infinite lists?

Let’s do a little trace in our heads of how Haskell will evaluate your expression. Substituting equals for equals on each line, the expression pretty quickly evaluates to True: myAny even [1..] foldr step False [1..] step 1 (foldr step False [2..]) even 1 || (foldr step False [2..]) False || (foldr step False [2..]) … Read more

Scala, repeat a finite list infinitely

Very similar to @Eastsun’s, but a bit more intention revealing. Tested in Scala 2.8. scala> val l = List(1, 2, 3) l: List[Int] = List(1, 2, 3) scala> Stream.continually(l.toStream).flatten.take(10).toList res3: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3, 1) Alternatively, with Scalaz: scala> import scalaz._ import scalaz._ scala> import Scalaz._ import Scalaz._ … Read more

How to tell if a list is infinite?

Applying length to unknown lists is generally a bad idea, both practically due to infinite lists, and conceptually because often it turns out that you don’t actually care about the length anyway. You said in a comment: I’m very new to Haskell, so now, don’t infinite structures make my programs very vulnerable? Not really. While … Read more

Javascript: “Infinite” parameters for function?

Functions can access an array-like object called arguments that contains all the arguments that they received function print_my_arguments(/**/){ var args = arguments; for(var i=0; i<args.length; i++){ console.log(args[i]); } }; And you can do the opposite conversion (call a function given a list of arguments) with the apply method: // These are equivalent: print_my_arguments(1,2,3); print_my_arguments.apply(null, [1,2,3]); … Read more

tech