Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Tyler Eaves <tyler@ml1.net>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Frustrated Beginner
Date: Tue, 23 Dec 2003 02:23:33 -0500	[thread overview]
Message-ID: <EA469D7C-3518-11D8-9480-000A9584A16E@ml1.net> (raw)
In-Reply-To: <B7F72AE6-3514-11D8-A8F7-000393CB0F1E@spy.net>

[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]


On Dec 23, 2003, at 1:53 AM, Dustin Sallings wrote:

>
> On Dec 22, 2003, at 22:26, Tyler Eaves wrote:
>
> (I'm sending this back to the list because, as you pointed out, there 
> were a few problems with that example, so here's one for the 
> archives).
>
>> Okay, so far so good. I take it that in O'Caml the overhead of a 
>> function call is much less than in, say, C? Otherwise this approach 
>> would seem to be rather inefficient.
>
> 	Well, the short answer is that that's not a function call.  :)  See 
> tail-recursion.
>
> 	It's also not the best example since it was so simple and didn't 
> return anything useful.  Consider this implementation of fold (roughly 
> translating it in this email from a scheme version I wrote on my 
> palm):
>
> let rec fold f init lst =
>     match lst with
>           [] -> init
>         | h::t -> fold f (f init h) t
> ;;
>
> 	If you're not familiar with fold, here's an example:
>
> fold (fun v c -> c + v) 0 [1;2;3;4;5;6;7;8;9;10]
>
> 	That invocation returns 55.  Basically, the function is called for 
> each value in the list (v) and the current value is passed in along 
> with the current value during the calculation (c).  When it gets to 
> the end (list is []), you return the init value.  Otherwise, you apply 
> the function.
>
> 	Note that this function could also be written as follows (and 
> probably would be):
>
> let rec fold f init = function
>       [] -> init
>     | h::t -> fold f (f init h) t
> ;;

This makes since in general. I'm guess h and t correspond to the head 
and tail of the lift. It'll probably make more sense tomorrow. It's 
2:11 here, and I'd planned to be asleep two hours ago. The replies have 
kept me up ;)

Based on what I've learned, I've tried to write a simple prime number 
finder. It doesn't actually *work* or anything (Syntax Error on line 
23, can't figu

[-- Attachment #2: primes.ml --]
[-- Type: application/octet-stream, Size: 690 bytes --]

(*  primes.ml
    build a list of prime numbers.
    Tyler Eaves <tyler@ml1.net> 
    Very naive and stupid algortihmn.
    It tries taking the modulo of each
    known prime with the test number.
    If modulo = 0 for any number, the
    test is not prime.
    *)
   
let primes = [2];;

(* Modulo tester *)
let fmod x y = x mod y;;

let rec isprime n = (
  let test = List.map (fmod n) primes;
  let testa = Array.of_list test;
  let testa = Array.sort compare testa;
  if testa.(0) = 0 then
    (* Not prime *)
    Printf.printf "%d is not prime.\n" n
    else 
    begin
    Printf.printf "%d IS PRIME!!\n" n;
    let primes = n :: primes 
    end
   );
  isprime n+1
  ;;

isprime 3;;

[-- Attachment #3: Type: text/plain, Size: 100 bytes --]

re out why...), but I think I've figured some of this stuff out, kinda. 
I'm encouraged at least :)

  reply	other threads:[~2003-12-23  7:23 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-23  4:03 Tyler Eaves
2003-12-23  4:19 ` jayanta nath
2003-12-23  5:34   ` Matt Gushee
2003-12-23  6:11     ` Tyler Eaves
2003-12-23  6:21       ` Michael Vanier
2003-12-23  6:31       ` Michael Jeffrey Tucker
2003-12-23 12:16         ` Richard Jones
2003-12-23 20:23           ` Dustin Sallings
2003-12-23  6:32       ` Shawn Wagner
2003-12-23  6:43       ` Matt Gushee
2003-12-23  5:58 ` Dustin Sallings
     [not found]   ` <EAEE2FF2-3510-11D8-B3A1-000A9584A16E@ml1.net>
2003-12-23  6:53     ` Dustin Sallings
2003-12-23  7:23       ` Tyler Eaves [this message]
2003-12-23  8:26         ` Dustin Sallings
2003-12-23  6:20 ` Tom Murray
2003-12-23  8:52 ` Stefano Zacchiroli
2003-12-23 16:47   ` [Caml-list] Ocaml syntax David Brown
2003-12-23 20:19     ` Dustin Sallings
2003-12-23 21:03       ` Eric Merritt
2003-12-23 21:52     ` brogoff
2003-12-24 10:27       ` skaller
2003-12-24 11:42         ` Peter Jolly
2003-12-24 12:19           ` skaller
2003-12-30  8:14     ` dmitry grebeniuk
2003-12-30 17:48       ` David Brown
2003-12-23 10:26 ` [Caml-list] Frustrated Beginner Samuel Lacas
2003-12-23 11:01   ` Dustin Sallings
2003-12-23 14:34     ` Oleg Trott
2003-12-23 20:25       ` Dustin Sallings
2003-12-23 16:11 ` Brian Hurt
2003-12-23 16:20   ` Sven Luther
2003-12-23 16:52     ` David Brown
2003-12-23 20:32       ` Dustin Sallings
2003-12-24 10:41         ` Issac Trotts
2003-12-23 17:39     ` Brian Hurt
2003-12-24  9:35       ` Jacques Garrigue
2003-12-24  9:49 ` skaller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=EA469D7C-3518-11D8-9480-000A9584A16E@ml1.net \
    --to=tyler@ml1.net \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox